| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""test anta.decorators.py.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import logging |
| 9 |
from typing import TYPE_CHECKING, ClassVar |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from anta.decorators import deprecated_test_class, skip_on_platforms |
| 14 |
from anta.models import AntaCommand, AntaTemplate, AntaTest |
| 15 |
|
| 16 |
if TYPE_CHECKING: |
| 17 |
from anta.device import AntaDevice |
| 18 |
|
| 19 |
|
| 20 |
class ExampleTest(AntaTest): |
| 21 |
"""ANTA test that always succeed.""" |
| 22 |
|
| 23 |
categories: ClassVar[list[str]] = [] |
| 24 |
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [] |
| 25 |
|
| 26 |
@AntaTest.anta_test |
| 27 |
def test(self) -> None: |
| 28 |
"""Test function.""" |
| 29 |
self.result.is_success() |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.parametrize( |
| 33 |
"new_tests", |
| 34 |
[ |
| 35 |
pytest.param(None, id="No new_tests"), |
| 36 |
pytest.param(["NewExampleTest"], id="one new_tests"), |
| 37 |
pytest.param(["NewExampleTest1", "NewExampleTest2"], id="multiple new_tests"), |
| 38 |
], |
| 39 |
) |
| 40 |
def test_deprecated_test_class(caplog: pytest.LogCaptureFixture, device: AntaDevice, new_tests: list[str] | None) -> None: |
| 41 |
"""Test deprecated_test_class decorator.""" |
| 42 |
caplog.set_level(logging.INFO) |
| 43 |
|
| 44 |
decorated_test_class = deprecated_test_class(new_tests=new_tests)(ExampleTest) |
| 45 |
|
| 46 |
|
| 47 |
decorated_test_class(device) |
| 48 |
|
| 49 |
if new_tests is None: |
| 50 |
assert "ExampleTest test is deprecated." in caplog.messages |
| 51 |
else: |
| 52 |
assert f"ExampleTest test is deprecated. Consider using the following new tests: {', '.join(new_tests)}." in caplog.messages |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.parametrize( |
| 56 |
("platforms", "device_platform", "expected_result"), |
| 57 |
[ |
| 58 |
pytest.param([], "cEOS-lab", "success", id="empty platforms"), |
| 59 |
pytest.param(["cEOS-lab"], "cEOS-lab", "skipped", id="skip on one platform - match"), |
| 60 |
pytest.param(["cEOS-lab"], "vEOS", "success", id="skip on one platform - no match"), |
| 61 |
pytest.param(["cEOS-lab", "vEOS"], "cEOS-lab", "skipped", id="skip on multiple platforms - match"), |
| 62 |
], |
| 63 |
) |
| 64 |
async def test_skip_on_platforms(device: AntaDevice, platforms: list[str], device_platform: str, expected_result: str) -> None: |
| 65 |
"""Test skip_on_platforms decorator. |
| 66 |
|
| 67 |
Leverage the ExampleTest defined at the top of the module. |
| 68 |
""" |
| 69 |
|
| 70 |
ExampleTest.test = skip_on_platforms(platforms)(ExampleTest.test) |
| 71 |
|
| 72 |
device.hw_model = device_platform |
| 73 |
|
| 74 |
test_instance = ExampleTest(device) |
| 75 |
await test_instance.test() |
| 76 |
|
| 77 |
assert test_instance.result.result == expected_result |