Skip to content

Commit a7743de

Browse files
authored
Refactoring noxfile for testing (#189)
* Use nox.options instead of global variable Move the declaration of the backend to a `nox.options` declaration instead of passing an extra variable to each `@session`. * 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. * Remove cache: pip option This is having a negative impact to the CI. Will investigate later
1 parent 6b34d20 commit a7743de

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

.github/workflows/python-tests.yml

Lines changed: 5 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
@@ -43,11 +43,11 @@ jobs:
4343

4444
- name: "Install nox"
4545
run: |
46-
python -m pip install --upgrade pip nox
46+
python -m pip install --upgrade nox
4747
4848
- name: "Run tests and coverage via nox"
4949
run: |
50-
nox --session version_coverage-${{ matrix.python-version }}
50+
nox --session test -- partial-coverage
5151
5252
- name: "Save coverage artifact"
5353
uses: "actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b"
@@ -58,7 +58,7 @@ jobs:
5858
include-hidden-files: true
5959

6060
coverage-compile:
61-
name: "coverage compile"
61+
name: "Compile coverage reports."
6262
needs: "run-tests-and-coverage"
6363
runs-on: "ubuntu-latest"
6464
steps:
@@ -72,7 +72,7 @@ jobs:
7272

7373
- name: "Install nox"
7474
run: |
75-
python -m pip install --upgrade pip nox
75+
python -m pip install --upgrade nox
7676
7777
- name: "Download coverage artifacts"
7878
uses: "actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16"

noxfile.py

Lines changed: 30 additions & 33 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,8 +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"]
16-
VENV_BACKEND = "venv"
1716
VENV_PATH = ".venv"
1817
REQUIREMENTS_PATH = "./requirements"
1918

@@ -32,11 +31,10 @@
3231
"./**/*.pyo",
3332
]
3433

35-
3634
# Define the default sessions run when `nox` is called on the CLI
35+
nox.options.default_venv_backend = "virtualenv"
3736
nox.options.sessions = [
38-
"version_coverage",
39-
"coverage_combine",
37+
"test",
4038
"mypy",
4139
]
4240

@@ -73,33 +71,40 @@ def dev(session: nox.Session) -> None:
7371
session.log(f"\n\nRun '{activate_command}' to enter the virtual environment.\n")
7472

7573

76-
@nox.session(python=PYTHON_MATRIX, venv_backend=VENV_BACKEND)
77-
def version_coverage(session: nox.Session) -> None:
78-
"""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."""
7977
print_standard_logs(session)
8078

8179
session.install(".")
82-
session.install("-r", "requirements/requirements.txt")
83-
session.install("-r", "requirements/requirements-test.txt")
84-
session.run("coverage", "run", "-p", "-m", "pytest", TESTS_PATH)
80+
session.install("-r", f"{REQUIREMENTS_PATH}/requirements-test.txt")
81+
82+
coverage = partial(session.run, "python", "-m", "coverage")
83+
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")
8592

8693

87-
@nox.session(python=DEFAULT_PYTHON, venv_backend=VENV_BACKEND)
94+
@nox.session()
8895
def coverage_combine(session: nox.Session) -> None:
89-
"""Combine all coverage partial files and generate JSON report."""
96+
"""CI: Combine parallel-mode coverage files and produce reports."""
9097
print_standard_logs(session)
9198

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

94-
session.install(".")
95-
session.install("-r", "requirements/requirements.txt")
96-
session.install("-r", "requirements/requirements-test.txt")
97-
session.run("python", "-m", "coverage", "combine")
98-
session.run("python", "-m", "coverage", "report", "-m", fail_under)
99-
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")
100105

101106

102-
@nox.session(python=DEFAULT_PYTHON, venv_backend=VENV_BACKEND)
107+
@nox.session(python=DEFAULT_PYTHON)
103108
def mypy(session: nox.Session) -> None:
104109
"""Run mypy against package and all required dependencies."""
105110
print_standard_logs(session)
@@ -110,15 +115,7 @@ def mypy(session: nox.Session) -> None:
110115
session.run("mypy", "-p", MODULE_NAME, "--no-incremental")
111116

112117

113-
@nox.session(python=False, venv_backend=VENV_BACKEND)
114-
def coverage(session: nox.Session) -> None:
115-
"""Generate a coverage report. Does not use a venv."""
116-
session.run("coverage", "erase")
117-
session.run("coverage", "run", "-m", "pytest", TESTS_PATH)
118-
session.run("coverage", "report", "-m")
119-
120-
121-
@nox.session(python=DEFAULT_PYTHON, venv_backend=VENV_BACKEND)
118+
@nox.session(python=DEFAULT_PYTHON)
122119
def build(session: nox.Session) -> None:
123120
"""Build distribution files."""
124121
print_standard_logs(session)
@@ -127,7 +124,7 @@ def build(session: nox.Session) -> None:
127124
session.run("python", "-m", "build")
128125

129126

130-
@nox.session(python=DEFAULT_PYTHON, venv_backend=VENV_BACKEND, name="update-deps")
127+
@nox.session(python=DEFAULT_PYTHON, name="update-deps")
131128
def update_deps(session: nox.Session) -> None:
132129
"""Process requirement*.txt files, updating only additions/removals."""
133130
print_standard_logs(session)
@@ -145,7 +142,7 @@ def update_deps(session: nox.Session) -> None:
145142
)
146143

147144

148-
@nox.session(python=DEFAULT_PYTHON, venv_backend=VENV_BACKEND, name="upgrade-deps")
145+
@nox.session(python=DEFAULT_PYTHON, name="upgrade-deps")
149146
def upgrade_deps(session: nox.Session) -> None:
150147
"""Process requirement*.txt files and upgrade all libraries as possible."""
151148
print_standard_logs(session)
@@ -164,7 +161,7 @@ def upgrade_deps(session: nox.Session) -> None:
164161
)
165162

166163

167-
@nox.session(python=False, venv_backend=VENV_BACKEND)
164+
@nox.session(python=False)
168165
def clean(_: nox.Session) -> None:
169166
"""Clean cache, .pyc, .pyo, and test/build artifact files from project."""
170167
count = 0

0 commit comments

Comments
 (0)