User Tools

Site Tools


automation:test-the-network

This is an old revision of the document!


hierarchy typically looks like:

  • Unit testing (atomic level) - individual functions/methods. (Module testing often gets lumped into either “unit testing” (for simple modules) or “component testing” (for complex modules))
  • Component testing - groups of related units working together
  • Integration testing - multiple components interacting
  • System testing - entire application

OPTIONS

Classic1: Unittest / **pytest** libraries

TODO

Modern1: PyATS

Assertions
  • Definition: Statements used in tests to verify that a specific condition holds true.
  • Usage: Typically done using Python's built-in
    assert

    statement.

  • Example:
    assert interface.status == "up"
 
  • Purpose: To validate correctness during test execution; a failed assertion marks the test as failed.

(@ is a decorator. special function that modifies the behavior of another function or class without changing its actual code)

Markers
  • Definition: Annotations to add metadata to test functions or classes.
  • Usage: Used to categorize, skip, or parametrize tests.
  • Examples:
    • @pytest.mark.sanity

      (categorization)

    • @pytest.mark.skip(reason="Not implemented")

      (skipping)

    • pyATS-specific:
      @aetest.loop

      ,

      @aetest.setup

      ,

      @aetest.cleanup
  • Purpose: Control test behavior, grouping, and execution.

Fixtures
  • Definition: Reusable setup/teardown functions for tests.
  • Usage: Fixtures are defined using the `@pytest.fixture` decorator and are injected automatically into test functions based on parameter names.
  • Provide setup logic for tests; Can include teardown logic after the test (using `yield`); Can be reused across multiple tests; Can depend on other fixtures; Can be parameterised

Example1:: The greeting fixture is provided automatically to both test functions.

import pytest
@pytest.fixture
def greeting():
    return "hello world"
def test_uppercase(greeting):
    assert greeting.upper() == "HELLO WORLD"
def test_length(greeting):
    assert len(greeting) == 11

Fixture with Setup and Teardown: The yield keyword pauses the fixture until the test finishes, then resumes for any cleanup.

import pytest
@pytest.fixture
def temp_file(tmp_path):
    file = tmp_path / "sample.txt"
    file.write_text("Hello")
    yield file
    # Cleanup code here (e.g. delete or log)

Dependent Fixtures : Fixtures can call other fixtures automatically.

@pytest.fixture
def user_data():
    return {"name": "Alice", "id": 1}
@pytest.fixture
def user_token(user_data):
    return f"TOKEN-{user_data['id']}"
def test_token(user_token):
    assert user_token.startswith("TOKEN-")
    

Parametrised Fixtures : The test runs once for each value of the user parameter.

@pytest.fixture(params=["alice", "bob"])
def user(request):
    return request.param
def test_starts_lowercase(user):
    assert user[0].islower()
    
    

Further Reading

  [[https://docs.pytest.org/en/stable/how-to/fixtures.html|Pytest Fixture Documentation]]
  [[https://realpython.com/pytest-python-testing/#using-pytest-fixtures|Real Python: Pytest Fixtures]]
  [[https://docs.pytest.org/en/stable/how-to/fixtures.html#parametrizing-fixtures|Parametrising Fixtures]]
  [[https://gist.github.com/kwaldrip/0ed22c6e3c8b476b8a84cf3c137b3e15|Pytest Fixture Cheat Sheet (Gist)]]

Parametrization
  • Definition: Technique to run the same test with different sets of parameters.
  • Usage: Implemented with
    @pytest.mark.parametrize

    .

  • Example:
    @pytest.mark.parametrize("ip", ["8.8.8.8", "1.1.1.1"])
    def test_ping(ip):
        assert ping(ip)
 
  • Purpose: Increase coverage by testing with multiple data sets.

Other Relevant Concepts
  • Testcase/Testscript:
    • A testcase is a Python class, often subclassing
      aetest.Testcase

      , containing test sections.

    • A testscript is a file containing one or more testcases.
  • Sections:
    • Special methods within a testcase, such as
      setup

      ,

      test

      , and

      cleanup

      .

  • Testbed:
    • A YAML or Python file describing devices and connections used in tests.
  • Loggers:
    • Used for reporting test steps and results (
      log

      object).

  • aetest:
    • Core pyATS test harness module for organizing and running tests.

Examples
automation/test-the-network.1751786208.txt.gz · Last modified: by jotasandoku