Streamline the "test" session

Rely on pyproject.toml for the `coverage` options,
and shorten the comments.
This commit is contained in:
Alexander Hess 2026-11-27 09:50:31 +01:00
commit 110c6f755b
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
2 changed files with 23 additions and 39 deletions

View file

@ -216,67 +216,40 @@ def lint_code(session: Session) -> None:
@nox.session(name='test', python=MAIN_PYTHON) @nox.session(name='test', python=MAIN_PYTHON)
def test_code(session: Session) -> None: def test_code(session: Session) -> None:
"""Test the code base. """Run the unit and integration tests with `pytest`.
Runs the unit and integration tests with pytest and
validate that all code snippets in docstrings work with xdoctest.
If no extra arguments are provided, the entire test suite If no extra arguments are provided, the entire test suite
is exexcuted and succeeds only with 100% coverage. is executed and succeeds only with 100% coverage.
Otherwise, they are forwarded to `pytest`.
If extra arguments are provided, they are
forwarded to pytest and xdoctest without any changes.
xdoctest ignores arguments it does not understand.
""" """
start(session) start(session)
# The testing tools require the developed package and its
# non-develop dependencies be installed in the virtual environment.
# As the package is not installed in editable mode, the coverage report
# relies on "[tool.coverage.paths]" in pyproject.toml to map to src/.
install_pinned( install_pinned(
session, session,
'.', # The tests import `velofy`, so it and its dependencies must be installed
'Faker', '.', # => A non-editable copy as it would really be installed
'factory-boy', 'factory-boy',
'Faker',
'packaging', 'packaging',
'pytest', 'pytest',
'pytest-cov', 'pytest-cov',
'pytest-env', 'pytest-env',
'pytest-mock', 'pytest-mock',
'xdoctest[optional]',
) )
session.run('pytest', '--version') session.run('pytest', '--version')
# Both R and a database are available locally and on the CI server. pytest_default_args = (
# Therefore, we require 100% coverage. # See "[tool.coverage]" in pyproject.toml for `coverage` settings,
pytest_args = ( # for example, branch coverage and report format,
# and "[tool.coverage.paths]" mapping `coverage` to src/
'--cov', '--cov',
'--no-cov-on-fail', '--no-cov-on-fail',
'--cov-branch',
'--cov-fail-under=100', '--cov-fail-under=100',
'--cov-report=term-missing:skip-covered',
PYTEST_LOCATION, PYTEST_LOCATION,
) )
# Interpret extra arguments as options for pytest. session.run('pytest', *(session.posargs or pytest_default_args))
posargs = session.posargs
session.run('pytest', *(posargs or pytest_args))
# For xdoctest, the default arguments are different from pytest.
args = posargs or [PACKAGE_IMPORT_NAME]
# The "TESTING" environment variable forces the global `engine`, `connection`,
# and `session` objects to be set to `None` and avoid any database connection.
# For `pytest` above, `pytest-env` sets it with the "env" option
# in the "[tool.pytest.ini_options]" section in pyproject.toml.
session.env['TESTING'] = 'true'
session.run('xdoctest', '--version')
session.run('xdoctest', '--quiet', *args) # --quiet => less verbose output
@nox.session(name='deps-audit', python=MAIN_PYTHON, reuse_venv=False) @nox.session(name='deps-audit', python=MAIN_PYTHON, reuse_venv=False)

View file

@ -451,10 +451,21 @@ ignore_missing_imports = true
cache_dir = ".cache/pytest" cache_dir = ".cache/pytest"
addopts = "--strict-markers" testpaths = ["tests"]
# "-ra" => Summarize skipped and `xfailed tests at the end
addopts = "-ra --strict-config --strict-markers"
console_output_style = "count" console_output_style = "count"
xfail_strict = true
required_plugins = [
"pytest-cov",
"pytest-env",
"pytest-mock",
]
env = [ env = [
"TESTING=true", "TESTING=true",
] ]