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.
This commit is contained in:
Alexander Hess 2026-12-06 11:39:56 +01:00
commit 93ba1a6398
Signed by: alexander
GPG key ID: B46EDB4DFC26805D

View file

@ -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)