Debian Codegraph @

tests/units/anta_tests/conftest.py

raw symbols SCIP
1 # Copyright (c) 2023-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 """See https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files."""
5
6 from typing import Any
7
8 import pytest
9
10
11 def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
12 """Generate ANTA tests unit tests dynamically during test collection.
13
14 It will parametrize test cases based on the `DATA` data structure defined in `tests.units.anta_tests` modules.
15 See `tests/units/anta_tests/README.md` for more information on how to use it.
16
17 Checking that only the function "test" is parametrized with data to allow for writing tests for helper functions
18 in each module.
19 """
20 assert metafunc.module is not None
21 if "tests.units.anta_tests" in metafunc.module.__package__ and metafunc.function.__name__ == "test":
22 if not hasattr(metafunc.module, "DATA"):
23 msg = "Tried to generate tests for {metafunc.module} but could not find 'DATA'."
24 raise RuntimeError(msg)
25
26 # This is a unit test for an AntaTest subclass
27 # Extract the test class, the unit test name and test data from the nested structure AntaUnitTestData
28 test_data = tuple((x[0][0], x[1]) for x in metafunc.module.DATA.items())
29 metafunc.parametrize(
30 "anta_test,unit_test_data",
31 test_data,
32 ids=[f"{anta_test.__module__}.{anta_test.__name__}-{unit_test_name}" for (anta_test, unit_test_name) in metafunc.module.DATA],
33 )