Skip to content

Instantly share code, notes, and snippets.

@codeinthehole
Created May 24, 2024 09:33
Show Gist options
  • Save codeinthehole/f6663121c3dca9177332505989b698af to your computer and use it in GitHub Desktop.
Save codeinthehole/f6663121c3dca9177332505989b698af to your computer and use it in GitHub Desktop.
A `noxfile.py` for running matrix testing of a Python package
import os
import time
import nox
@nox.session(python=["3.10", "3.11", "3.12"])
@nox.parametrize("django_constraint", ["<4.2", "<4.3", "<5.1"])
def tests(session: nox.Session, django_constraint: str) -> None:
"""
Run the test suite with different Python and Django versions.
"""
# Create a constraints file with the parameterized package versions.
# It's easy to add more constraints here if needed.
with open("constraints.txt", "w") as f:
f.write(f"django{django_constraint}\n")
# Install pip tools which we need to compile a new lock file.
session.install("pip-tools==7.4.1")
# Compile a new development lock file with the additional package constraints from this
# session. Use a unique lock file name to avoid session pollution.
lockfile = f"packages.{time.time()}.txt"
session.run(
"pip-compile",
"--quiet",
"--resolver=backtracking",
"--strip-extras",
"--extra=dev",
"pyproject.toml",
"--constraint",
"constraints.txt",
"--output-file",
lockfile,
)
# Install the dependencies from the newly compiled lockfile and main package.
session.install("-r", lockfile, ".")
# When run in CircleCI, create JUnit XML test results.
commands = ["pytest"]
if "CIRCLECI" in os.environ:
commands.append(f"--junitxml=test-results/junit.{session.name}.xml")
session.run(*commands, *session.posargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment