From 644beb86964fcb29c3f3b664d44a07b73c062b2c Mon Sep 17 00:00:00 2001 From: Tiara Rodney Date: Fri, 20 Jun 2025 21:47:17 +0200 Subject: [PATCH] feat(testing): init pytest fixtures current_test fixture allows to retrieve the current test context, that is exposed through the shell environment --- Makefile | 3 +++ Pipfile | 1 + src/byteb4rb1e/utils/testing/pytest/fixtures.py | 14 ++++++++++++++ .../utils/testing/pytest/test_fixtures.py | 16 ++++++++++++++++ tests/integration/conftest.py | 5 +++++ tox.ini | 14 ++++++++++++-- 6 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/byteb4rb1e/utils/testing/pytest/fixtures.py create mode 100644 tests/integration/byteb4rb1e/utils/testing/pytest/test_fixtures.py create mode 100644 tests/integration/conftest.py diff --git a/Makefile b/Makefile index f1b82b3..0a1ec21 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ test-reports: test-reports/unit test-reports/static test-reports/unit: python3 -m pipenv run -v test-unit +test-reports/integration: + python3 -m pipenv run -v test-integration + test-reports/static: python3 -m pipenv run -v test-static diff --git a/Pipfile b/Pipfile index d693e3e..fa10d4e 100644 --- a/Pipfile +++ b/Pipfile @@ -17,3 +17,4 @@ python_version = "3.11" "build" = "python3 -m build" "test-static" = "tox run -m static" "test-unit" = "tox run -m unit" +"test-integration" = "tox run -m integration" diff --git a/src/byteb4rb1e/utils/testing/pytest/fixtures.py b/src/byteb4rb1e/utils/testing/pytest/fixtures.py new file mode 100644 index 0000000..7415a22 --- /dev/null +++ b/src/byteb4rb1e/utils/testing/pytest/fixtures.py @@ -0,0 +1,14 @@ +import os +from pathlib import Path +from typing import Tuple + +import pytest + + +@pytest.fixture +def current_test() -> Tuple[Path, str]: + """ + """ + suite_path, case_name = os.getenv('PYTEST_CURRENT_TEST').split('::', 1) + case_name = case_name.split(' ', 1)[0] + return Path(suite_path).resolve(), case_name diff --git a/tests/integration/byteb4rb1e/utils/testing/pytest/test_fixtures.py b/tests/integration/byteb4rb1e/utils/testing/pytest/test_fixtures.py new file mode 100644 index 0000000..56302e5 --- /dev/null +++ b/tests/integration/byteb4rb1e/utils/testing/pytest/test_fixtures.py @@ -0,0 +1,16 @@ +from pathlib import Path + +import pytest + +pytestmark = pytest.mark.pytest + +from byteb4rb1e.utils.testing.pytest.fixtures import current_test + + +def test_current_test(current_test): + """ + """ + suite_path, case_name = current_test + + assert str(Path(__file__)) == str(suite_path) + assert case_name == "test_current_test" diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py new file mode 100644 index 0000000..7c7aa51 --- /dev/null +++ b/tests/integration/conftest.py @@ -0,0 +1,5 @@ +def pytest_configure(config): + # register an additional marker + config.addinivalue_line( + "markers", "pytest: test pytest integration" + ) diff --git a/tox.ini b/tox.ini index efb3353..0f03dd8 100644 --- a/tox.ini +++ b/tox.ini @@ -2,7 +2,8 @@ requires = tox>=4.19 env_list = - py3{8-12}-{unit} + unit-py3{9-13} + integration-py3{9-13}-pytest8 lint format @@ -34,7 +35,7 @@ deps = commands = black --check src tests -[testenv:py3{9-13}-unit] +[testenv:unit-py3{9-13}] description = run type check on code base labels = unit deps = @@ -42,3 +43,12 @@ deps = pytest commands = pytest tests/unit --junitxml=test-reports/{env_name}.xml + +[testenv:integration-py3{9-13}-pytest8] +description = run pytest integration tests +labels = integration +deps = + {[testenv]deps} + pytest8: pytest>=8.0,<=9.0 +commands = + pytest tests/integration -m pytest --junitxml=test-reports/{env_name}.xml