Skip to content

Commit e5ecc46

Browse files
committed
Refactor running tests with nox
Instead of having a session that handles a large matrix of python versions, use a session that leverages the default python. This reduces the overhead of maintaining the `noxfile.py`. The replacement session has an optional posarg `partial-coverage` which will produce parallel-mode coverage files. This creates drop-in replacement behavior for the GitHub action which runs the tests on its own matrix of python versions and operating systems. `coverage_combine` moves to a CI only session.
1 parent 8c35f2f commit e5ecc46

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

.github/workflows/python-tests.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ on:
1515

1616
jobs:
1717
run-tests-and-coverage:
18-
name: "Run nox for tests and coverage"
18+
name: "Run pytest with coverage."
1919
runs-on: "${{ matrix.os }}"
2020
strategy:
2121
fail-fast: false
@@ -40,14 +40,15 @@ jobs:
4040
with:
4141
python-version: "${{ matrix.python-version }}"
4242
allow-prereleases: true
43+
cache: "pip"
4344

4445
- name: "Install nox"
4546
run: |
46-
python -m pip install --upgrade pip nox
47+
python -m pip install --upgrade nox
4748
4849
- name: "Run tests and coverage via nox"
4950
run: |
50-
nox --session version_coverage-${{ matrix.python-version }}
51+
nox --session test -- partial-coverage
5152
5253
- name: "Save coverage artifact"
5354
uses: "actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b"
@@ -58,7 +59,7 @@ jobs:
5859
include-hidden-files: true
5960

6061
coverage-compile:
61-
name: "coverage compile"
62+
name: "Compile coverage reports."
6263
needs: "run-tests-and-coverage"
6364
runs-on: "ubuntu-latest"
6465
steps:
@@ -69,10 +70,11 @@ jobs:
6970
uses: "actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b"
7071
with:
7172
python-version: "3.12"
73+
cache: "pip"
7274

7375
- name: "Install nox"
7476
run: |
75-
python -m pip install --upgrade pip nox
77+
python -m pip install --upgrade nox
7678
7779
- name: "Download coverage artifacts"
7880
uses: "actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16"

noxfile.py

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pathlib
55
import shutil
66
import sys
7+
from functools import partial
78

89
import nox
910

@@ -12,7 +13,6 @@
1213
TESTS_PATH = "tests"
1314
COVERAGE_FAIL_UNDER = 50
1415
DEFAULT_PYTHON = "3.12"
15-
PYTHON_MATRIX = ["3.9", "3.10", "3.11", "3.12", "3.13"]
1616
VENV_PATH = ".venv"
1717
REQUIREMENTS_PATH = "./requirements"
1818

@@ -32,10 +32,9 @@
3232
]
3333

3434
# Define the default sessions run when `nox` is called on the CLI
35-
nox.options.default_venv_backend = "uv|virtualenv"
35+
nox.options.default_venv_backend = "virtualenv"
3636
nox.options.sessions = [
37-
"version_coverage",
38-
"coverage_combine",
37+
"test",
3938
"mypy",
4039
]
4140

@@ -72,30 +71,37 @@ def dev(session: nox.Session) -> None:
7271
session.log(f"\n\nRun '{activate_command}' to enter the virtual environment.\n")
7372

7473

75-
@nox.session(python=PYTHON_MATRIX)
76-
def version_coverage(session: nox.Session) -> None:
77-
"""Run unit tests with coverage saved to partial file."""
74+
@nox.session(name="test")
75+
def run_tests_with_coverage(session: nox.Session) -> None:
76+
"""Run pytest with coverage, outputs console report and json."""
7877
print_standard_logs(session)
7978

8079
session.install(".")
81-
session.install("-r", "requirements/requirements.txt")
82-
session.install("-r", "requirements/requirements-test.txt")
83-
session.run("coverage", "run", "-p", "-m", "pytest", TESTS_PATH)
80+
session.install("-r", f"{REQUIREMENTS_PATH}/requirements-test.txt")
8481

82+
coverage = partial(session.run, "python", "-m", "coverage")
8583

86-
@nox.session(python=DEFAULT_PYTHON)
84+
coverage("erase")
85+
86+
if "partial-coverage" in session.posargs:
87+
coverage("run", "--parallel-mode", "--module", "pytest", TESTS_PATH)
88+
else:
89+
coverage("run", "--module", "pytest", TESTS_PATH)
90+
coverage("report", "--show-missing", f"--fail-under={COVERAGE_FAIL_UNDER}")
91+
coverage("json")
92+
93+
94+
@nox.session()
8795
def coverage_combine(session: nox.Session) -> None:
88-
"""Combine all coverage partial files and generate JSON report."""
96+
"""CI: Combine parallel-mode coverage files and produce reports."""
8997
print_standard_logs(session)
9098

91-
fail_under = f"--fail-under={COVERAGE_FAIL_UNDER}"
99+
session.install("-r", f"{REQUIREMENTS_PATH}/requirements-test.txt")
92100

93-
session.install(".")
94-
session.install("-r", "requirements/requirements.txt")
95-
session.install("-r", "requirements/requirements-test.txt")
96-
session.run("python", "-m", "coverage", "combine")
97-
session.run("python", "-m", "coverage", "report", "-m", fail_under)
98-
session.run("python", "-m", "coverage", "json")
101+
coverage = partial(session.run, "python", "-m", "coverage")
102+
coverage("combine")
103+
coverage("report", "--show-missing", f"--fail-under={COVERAGE_FAIL_UNDER}")
104+
coverage("json")
99105

100106

101107
@nox.session(python=DEFAULT_PYTHON)
@@ -109,14 +115,6 @@ def mypy(session: nox.Session) -> None:
109115
session.run("mypy", "-p", MODULE_NAME, "--no-incremental")
110116

111117

112-
@nox.session(python=False)
113-
def coverage(session: nox.Session) -> None:
114-
"""Generate a coverage report. Does not use a venv."""
115-
session.run("coverage", "erase")
116-
session.run("coverage", "run", "-m", "pytest", TESTS_PATH)
117-
session.run("coverage", "report", "-m")
118-
119-
120118
@nox.session(python=DEFAULT_PYTHON)
121119
def build(session: nox.Session) -> None:
122120
"""Build distribution files."""

0 commit comments

Comments
 (0)