Skip to content

Commit d9ded2d

Browse files
committed
Always generate JSON summaries for xdist workers
1 parent 8659069 commit d9ded2d

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

pytest_mpl/plugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,12 +1008,13 @@ def pytest_sessionfinish(self, session):
10081008
json.dump(result_hashes, fp, indent=2)
10091009

10101010
if self.generate_summary:
1011+
if is_xdist_worker:
1012+
self.generate_summary_json()
1013+
return
10111014
kwargs = {}
10121015
if 'json' in self.generate_summary:
10131016
summary = self.generate_summary_json()
10141017
print(f"A JSON report can be found at: {summary}")
1015-
if is_xdist_worker:
1016-
return
10171018
if result_hash_library.exists(): # link to it in the HTML
10181019
kwargs["hash_library"] = result_hash_library.name
10191020
if 'html' in self.generate_summary:

tests/subtests/test_subtest.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@
4747
]
4848

4949

50+
def xdist_args(n_workers):
51+
try:
52+
import xdist
53+
if n_workers is None:
54+
return ["-p", "no:xdist"]
55+
else:
56+
return ["-n", str(n_workers)]
57+
except ImportError:
58+
return []
59+
60+
5061
def run_subtest(baseline_summary_name, tmp_path, args, summaries=None, xfail=True,
5162
has_result_hashes=False, generating_hashes=False, testing_hashes=False, n_xdist_workers=None,
5263
update_baseline=UPDATE_BASELINE, update_summary=UPDATE_SUMMARY):
@@ -112,14 +123,7 @@ def run_subtest(baseline_summary_name, tmp_path, args, summaries=None, xfail=Tru
112123
shutil.copy(expected_result_hash_library, baseline_hash_library)
113124
transform_hashes(baseline_hash_library)
114125

115-
try:
116-
import xdist
117-
if n_xdist_workers is None:
118-
pytest_args += ["-p", "no:xdist"]
119-
else:
120-
pytest_args += ["-n", str(n_xdist_workers)]
121-
except ImportError:
122-
pass
126+
pytest_args.extend(xdist_args(n_xdist_workers))
123127

124128
# Run the test and record exit status
125129
status = subprocess.call(pytest_args + mpl_args + args)
@@ -363,3 +367,19 @@ def test_html_run_generate_hashes_only(tmp_path):
363367
# Run a hybrid mode test last so if generating hash libraries, it includes all the hashes.
364368
def test_hybrid(tmp_path):
365369
run_subtest('test_hybrid', tmp_path, [HASH_LIBRARY_FLAG, BASELINE_IMAGES_FLAG_ABS], testing_hashes=True)
370+
371+
372+
@pytest.mark.parametrize("num_workers", [None, 0, 1, 2])
373+
def test_html_no_json(tmp_path, num_workers):
374+
# Previous tests require JSON summary to be generated to function correctly.
375+
# This test ensures HTML summary generation works without JSON summary.
376+
results_path = tmp_path / 'results'
377+
results_path.mkdir()
378+
mpl_args = ['--mpl', rf'--mpl-results-path={results_path.as_posix()}',
379+
'--mpl-generate-summary=html', *xdist_args(num_workers)]
380+
subprocess.call([sys.executable, '-m', 'pytest', str(TEST_FILE), *mpl_args])
381+
assert not (tmp_path / 'results' / 'results.json').exists()
382+
html_path = tmp_path / 'results' / 'fig_comparison.html'
383+
assert html_path.exists()
384+
assert html_path.stat().st_size > 200_000
385+
assert "Baseline image differs" in html_path.read_text()

0 commit comments

Comments
 (0)