Skip to content

Commit 73dd8bf

Browse files
committed
Initial options for summary and named results dir
1 parent d736212 commit 73dd8bf

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

pytest_mpl/plugin.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ def pytest_addoption(parser):
109109
group.addoption('--mpl-hash-library',
110110
help="json library of image hashes, relative to "
111111
"location where py.test is run", action='store')
112+
group.addoption('--mpl-generate-summary', action='store_true',
113+
help="Generate a summary HTML report of any failed tests"
114+
", in --mpl-results-path")
112115

113116
results_path_help = "directory for test results, relative to location where py.test is run"
114117
group.addoption('--mpl-results-path', help=results_path_help, action='store')
@@ -130,6 +133,7 @@ def pytest_configure(config):
130133
generate_hash_lib = config.getoption("--mpl-generate-hash-library")
131134
results_dir = config.getoption("--mpl-results-path") or config.getini("mpl-results-path")
132135
hash_library = config.getoption("--mpl-hash-library")
136+
generate_summary = config.getoption("--mpl-generate-summary")
133137

134138
if config.getoption("--mpl-baseline-relative"):
135139
baseline_relative_dir = config.getoption("--mpl-baseline-path")
@@ -159,7 +163,8 @@ def pytest_configure(config):
159163
generate_dir=generate_dir,
160164
results_dir=results_dir,
161165
hash_library=hash_library,
162-
generate_hash_library=generate_hash_lib))
166+
generate_hash_library=generate_hash_lib,
167+
generate_summary=generate_summary))
163168

164169
else:
165170

@@ -215,7 +220,8 @@ def __init__(self,
215220
generate_dir=None,
216221
results_dir=None,
217222
hash_library=None,
218-
generate_hash_library=None
223+
generate_hash_library=None,
224+
generate_summary=False
219225
):
220226
self.config = config
221227
self.baseline_dir = baseline_dir
@@ -224,8 +230,12 @@ def __init__(self,
224230
self.results_dir = path_is_not_none(results_dir)
225231
self.hash_library = path_is_not_none(hash_library)
226232
self.generate_hash_library = path_is_not_none(generate_hash_library)
227-
if self.results_dir and not self.results_dir.exists():
228-
self.results_dir.mkdir()
233+
self.generate_summary = bool(generate_summary)
234+
235+
# Generate the containing dir for all test results
236+
if not self.results_dir:
237+
self.results_dir = Path(tempfile.mkdtemp(dir=self.results_dir))
238+
self.results_dir.mkdir(parents=True, exist_ok=True)
229239

230240
# We need global state to store all the hashes generated over the run
231241
self._generated_hash_library = {}
@@ -251,11 +261,20 @@ def generate_filename(self, item):
251261

252262
return filename
253263

254-
def make_results_dir(self, item):
264+
def generate_test_name(self, item):
265+
"""
266+
Generate a unique name for the hash for this test.
267+
"""
268+
return f"{item.module.__name__}.{item.name}"
269+
270+
def make_test_results_dir(self, item):
255271
"""
256272
Generate the directory to put the results in.
257273
"""
258-
return Path(tempfile.mkdtemp(dir=self.results_dir))
274+
test_name = self.generate_test_name(item)
275+
results_dir = self.results_dir / test_name
276+
results_dir.mkdir(exist_ok=True, parents=True)
277+
return results_dir
259278

260279
def baseline_directory_specified(self, item):
261280
"""
@@ -328,12 +347,6 @@ def generate_baseline_image(self, item, fig):
328347
close_mpl_figure(fig)
329348
pytest.skip("Skipping test, since generating image")
330349

331-
def generate_hash_name(self, item):
332-
"""
333-
Generate a unique name for the hash for this test.
334-
"""
335-
return f"{item.module.__name__}.{item.name}"
336-
337350
def generate_image_hash(self, item, fig):
338351
"""
339352
For a `matplotlib.figure.Figure`, returns the SHA256 hash as a hexadecimal
@@ -365,7 +378,7 @@ def compare_image_to_baseline(self, item, fig, result_dir):
365378

366379
baseline_image_ref = self.obtain_baseline_image(item, result_dir)
367380

368-
test_image = (result_dir / self.generate_filename(item)).absolute()
381+
test_image = (result_dir / "result.png").absolute()
369382
fig.savefig(str(test_image), **savefig_kwargs)
370383

371384
if not os.path.exists(baseline_image_ref):
@@ -377,7 +390,7 @@ def compare_image_to_baseline(self, item, fig, result_dir):
377390

378391
# distutils may put the baseline images in non-accessible places,
379392
# copy to our tmpdir to be sure to keep them in case of failure
380-
baseline_image = (result_dir / f"baseline-{self.generate_filename(item)}").absolute()
393+
baseline_image = (result_dir / "baseline.png").absolute()
381394
shutil.copyfile(baseline_image_ref, baseline_image)
382395

383396
# Compare image size ourselves since the Matplotlib
@@ -407,7 +420,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir):
407420
pytest.fail(f"Can't find hash library at path {hash_library_filename}")
408421

409422
hash_library = self.load_hash_library(hash_library_filename)
410-
hash_name = self.generate_hash_name(item)
423+
hash_name = self.generate_test_name(item)
411424

412425
if hash_name not in hash_library:
413426
return f"Hash for test '{hash_name}' not found in {hash_library_filename}."
@@ -489,12 +502,12 @@ def item_function_wrapper(*args, **kwargs):
489502
self.generate_baseline_image(item, fig)
490503

491504
if self.generate_hash_library is not None:
492-
hash_name = self.generate_hash_name(item)
505+
hash_name = self.generate_test_name(item)
493506
self._generated_hash_library[hash_name] = self.generate_image_hash(item, fig)
494507

495508
# Only test figures if we are not generating hashes or images
496509
if self.generate_dir is None and self.generate_hash_library is None:
497-
result_dir = self.make_results_dir(item)
510+
result_dir = self.make_test_results_dir(item)
498511

499512
# Compare to hash library
500513
if self.hash_library or compare.kwargs.get('hash_library', None):
@@ -507,7 +520,8 @@ def item_function_wrapper(*args, **kwargs):
507520
close_mpl_figure(fig)
508521

509522
if msg is None:
510-
shutil.rmtree(result_dir)
523+
pass
524+
# shutil.rmtree(result_dir)
511525
else:
512526
pytest.fail(msg, pytrace=False)
513527

@@ -528,6 +542,9 @@ def pytest_unconfigure(self, config):
528542
with open(hash_library_path, "w") as fp:
529543
json.dump(self._generated_hash_library, fp, indent=2)
530544

545+
if self.generate_summary:
546+
breakpoint()
547+
531548

532549
class FigureCloser(object):
533550
"""

tests/test_pytest_mpl.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,15 @@ def test_output_dir(tmpdir):
148148
with open(test_file, 'w') as f:
149149
f.write(TEST_OUTPUT_DIR)
150150

151-
output_dir = tmpdir.join('test_output_dir').strpath
151+
output_dir = tmpdir.join('test_output_dir')
152152

153153
# When we run the test, we should get output images where we specify
154154
code = call_pytest([f'--mpl-results-path={output_dir}',
155155
'--mpl', test_file])
156156

157157
assert code != 0
158-
assert os.path.exists(output_dir)
159-
160-
# Listdir() is to get the random name that the output for the one test is written into
161-
assert os.path.exists(os.path.join(output_dir, os.listdir(output_dir)[0],
162-
'test_output_dir.png'))
158+
assert output_dir.exists()
159+
assert (output_dir / "test.test_output_dir" / "result.png").exists()
163160

164161

165162
TEST_GENERATE = """

0 commit comments

Comments
 (0)