From 93ba1a639841a281c00278fcff6ac68e30f36805 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Sun, 6 Dec 2026 11:39:56 +0100 Subject: [PATCH] Pull out the common steps of the `pytest` sessions The "test", "test-fast", and "test-types" sessions all start the `session`, install the `TEST_DEPENDENCIES`, show the version, and run `pytest`. The new `run_pytest()` does all of that, and the `session`s only pass their arguments. --- noxfile.py | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/noxfile.py b/noxfile.py index e435dab..7cc79fb 100644 --- a/noxfile.py +++ b/noxfile.py @@ -106,7 +106,7 @@ SCRIPTS_LOCATION = "scripts/" TESTS_LOCATION = "tests/" -# Packages the "test" and "test-types" sessions need to run the test suite +# Packages `run_pytest()` installs for the test suite TEST_DEPENDENCIES = ( # The tests import `velofy`, so it and its dependencies must be installed ".", # => A non-editable copy as it would really be installed @@ -309,12 +309,10 @@ def test_code(session: nox.Session) -> None: is executed and succeeds only with 100% coverage. Otherwise, they are forwarded to `pytest`. """ - start(session) - - install_pinned(session, *TEST_DEPENDENCIES) - - session.run("pytest", "--version") - session.run("pytest", *(session.posargs or TEST_DEFAULT_ARGS)) + run_pytest( + session, + *(session.posargs or TEST_DEFAULT_ARGS), + ) @nox.session(name="test-fast", python=MAIN_PYTHON) @@ -328,15 +326,10 @@ def test_code_fast(session: nox.Session) -> None: Extra arguments are forwarded to `pytest`. """ - start(session) - - install_pinned(session, *TEST_DEPENDENCIES) - # Must match the "sequentially" param of `db_connection` in tests/conftest.py # => Otherwise, `-k` deselects nothing, and the entire test suite runs - session.run("pytest", "--version") - session.run( - "pytest", + run_pytest( + session, "-k", "not sequentially", *(session.posargs or TEST_DEFAULT_ARGS), @@ -379,15 +372,14 @@ def test_types(session: nox.Session) -> None: Extra arguments are forwarded to `pytest`. """ - start(session) - - install_pinned(session, *TEST_DEPENDENCIES, "typeguard") - # Make tests/__init__.py install the import hook for `typeguard` session.env["TYPEGUARD"] = "true" - session.run("pytest", "--version") - session.run("pytest", *(session.posargs or (TESTS_LOCATION,))) + run_pytest( + session, + *(session.posargs or (TESTS_LOCATION,)), + extra_dependencies=("typeguard",), + ) # This is a separate session on purpose @@ -866,6 +858,24 @@ def skip_install(session: nox.Session) -> bool: return bool(no_install and venv_reused) +def run_pytest( + session: nox.Session, + *pytest_args: str, + extra_dependencies: tuple[str, ...] = (), +) -> None: + """Install the `TEST_DEPENDENCIES` and run `pytest` with `pytest_args`.""" + start(session) + + install_pinned( + session, + *TEST_DEPENDENCIES, + *extra_dependencies, + ) + + session.run("pytest", "--version") + session.run("pytest", *pytest_args) + + if MAIN_PYTHON not in SUPPORTED_PYTHONS: msg = f"MAIN_PYTHON version, v{MAIN_PYTHON}, is not in SUPPORTED_PYTHONS" raise RuntimeError(msg)