Skip to content

Commit 1c7b5e0

Browse files
committed
test: more focused .pth file creation
This removes our custom balanced_xdist plugin. We couldn't force a set of tests onto one worked, and xdist now lets us do that with xdist_group(). The tests run about 5% slower.
1 parent 89b0117 commit 1c7b5e0

File tree

7 files changed

+32
-243
lines changed

7 files changed

+32
-243
lines changed

pyproject.toml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ ignored-argument-names = "_|unused|.*_unused"
111111
## PYTEST
112112

113113
[tool.pytest.ini_options]
114-
addopts = "-q -n auto -p no:legacypath --strict-markers --no-flaky-report -rfEX --failed-first"
114+
addopts = "-q -n auto --dist=loadgroup -p no:legacypath --strict-markers --no-flaky-report -rfEX --failed-first"
115115
python_classes = "*Test"
116116
markers = [
117117
"expensive: too slow to run during \"make smoke\"",
@@ -138,15 +138,6 @@ xfail_strict = true
138138
# https://docs.pytest.org/en/stable/reference/reference.html#confval-verbosity_assertions
139139
verbosity_assertions = 5
140140

141-
balanced_clumps = [
142-
# Because of expensive session-scoped fixture:
143-
"VirtualenvTest",
144-
# Because of shared-file manipulations (~/tests/actual/testing):
145-
"CompareTest",
146-
# No idea why this one fails if run on separate workers:
147-
"GetZipBytesTest",
148-
]
149-
150141
## Scriv
151142

152143
[tool.scriv]

tests/balance_xdist_plugin.py

Lines changed: 0 additions & 191 deletions
This file was deleted.

tests/conftest.py

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
# $set_env.py: PYTEST_ADDOPTS - Extra arguments to pytest.
3131

3232
pytest_plugins = [
33-
"tests.balance_xdist_plugin",
3433
"tests.select_plugin",
3534
]
3635

@@ -94,29 +93,22 @@ def force_local_pyc_files() -> None:
9493
sys.pycache_prefix = None
9594

9695

97-
WORKER = os.getenv("PYTEST_XDIST_WORKER", "none")
98-
99-
def pytest_sessionstart() -> None:
100-
"""Run once at the start of the test session."""
101-
warnings.filterwarnings("ignore", r".*no-sysmon")
102-
# Only in the main process...
103-
if WORKER == "none":
104-
# Create a .pth file for measuring subprocess coverage.
105-
pth_dir = find_writable_pth_directory()
106-
assert pth_dir
107-
sub_dir = pth_dir / "subcover.pth"
108-
sub_dir.write_text("import coverage; coverage.process_startup()\n", encoding="utf-8")
109-
# subcover.pth is deleted by pytest_sessionfinish below.
110-
111-
112-
def pytest_sessionfinish() -> None:
113-
"""Hook the end of a test session, to clean up."""
114-
# This is called by each of the workers and by the main process.
115-
if WORKER == "none":
116-
for pth_dir in possible_pth_dirs(): # pragma: part covered
117-
pth_file = pth_dir / "subcover.pth"
118-
if pth_file.exists():
119-
pth_file.unlink()
96+
# Give this an underscored name so pylint won't complain when we use the fixture.
97+
@pytest.fixture(name="_create_pth_file")
98+
def create_pth_file() -> Iterator[None]:
99+
"""Create and clean up a .pth file for tests that need it for subprocesses."""
100+
for pth_dir in possible_pth_dirs(): # pragma: part covered
101+
pth_file = pth_dir / "subcover.pth"
102+
try:
103+
pth_file.write_text("import coverage; coverage.process_startup()\n", encoding="utf-8")
104+
except OSError: # pragma: cant happen
105+
continue
106+
else:
107+
break
108+
try:
109+
yield
110+
finally:
111+
pth_file.unlink()
120112

121113

122114
def possible_pth_dirs() -> Iterator[Path]:
@@ -131,18 +123,3 @@ def possible_pth_dirs() -> Iterator[Path]:
131123
# If we're still looking, then try the Python library directory.
132124
# https://github.com/nedbat/coveragepy/issues/339
133125
yield Path(sysconfig.get_path("purelib")) # pragma: cant happen
134-
135-
136-
def find_writable_pth_directory() -> Path | None:
137-
"""Find a place to write a .pth file."""
138-
for pth_dir in possible_pth_dirs(): # pragma: part covered
139-
try_it = pth_dir / f"touch_{WORKER}.it"
140-
try:
141-
try_it.write_text("foo", encoding="utf-8")
142-
except OSError: # pragma: cant happen
143-
continue
144-
145-
os.remove(try_it)
146-
return pth_dir
147-
148-
return None # pragma: cant happen

tests/test_goldtest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def path_regex(path: str) -> str:
4747
GOLD_PATH_RX = path_regex("/tests/gold/testing/getty/gettysburg.txt")
4848
OUT_PATH_RX = path_regex("out/gettysburg.txt")
4949

50+
@pytest.mark.xdist_group(name="compare_test")
5051
class CompareTest(CoverageTest):
5152
"""Tests of goldtest.py:compare()"""
5253

tests/test_process.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,8 @@ def setUp(self) -> None:
13061306
f.close()
13071307
""")
13081308

1309-
def test_subprocess_with_pth_files(self) -> None:
1309+
@pytest.mark.xdist_group(name="needs_pth")
1310+
def test_subprocess_with_pth_files(self, _create_pth_file: None) -> None:
13101311
# An existing data file should not be read when a subprocess gets
13111312
# measured automatically. Create the data file here with bogus data in
13121313
# it.
@@ -1330,7 +1331,8 @@ def test_subprocess_with_pth_files(self) -> None:
13301331
data.read()
13311332
assert line_counts(data)['sub.py'] == 3
13321333

1333-
def test_subprocess_with_pth_files_and_parallel(self) -> None:
1334+
@pytest.mark.xdist_group(name="needs_pth")
1335+
def test_subprocess_with_pth_files_and_parallel(self, _create_pth_file: None) -> None:
13341336
# https://github.com/nedbat/coveragepy/issues/492
13351337
self.make_file("coverage.ini", """\
13361338
[run]
@@ -1377,7 +1379,14 @@ class ProcessStartupWithSourceTest(CoverageTest):
13771379
@pytest.mark.parametrize("dashm", ["-m", ""])
13781380
@pytest.mark.parametrize("package", ["pkg", ""])
13791381
@pytest.mark.parametrize("source", ["main", "sub"])
1380-
def test_pth_and_source_work_together(self, dashm: str, package: str, source: str) -> None:
1382+
@pytest.mark.xdist_group(name="needs_pth")
1383+
def test_pth_and_source_work_together(
1384+
self,
1385+
dashm: str,
1386+
package: str,
1387+
source: str,
1388+
_create_pth_file: None,
1389+
) -> None:
13811390
"""Run the test for a particular combination of factors.
13821391
13831392
The arguments are all strings:

tests/test_python.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from tests.helpers import os_sep
1818

1919

20+
@pytest.mark.xdist_group(name="get_zip_bytes_test")
2021
class GetZipBytesTest(CoverageTest):
2122
"""Tests of `get_zip_bytes`."""
2223

tests/test_venv.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def coverage_command_fixture(request: pytest.FixtureRequest) -> str:
162162
return cast(str, request.param)
163163

164164

165+
@pytest.mark.xdist_group(name="virtualenv_test")
165166
class VirtualenvTest(CoverageTest):
166167
"""Tests of virtualenv considerations."""
167168

0 commit comments

Comments
 (0)