Skip to content

Commit 7766591

Browse files
authored
Merge pull request #39 from dopplershift/output_dir
Control results directory
2 parents 5343109 + c1edb78 commit 7766591

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

pytest_mpl/plugin.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,47 @@ def pytest_addoption(parser):
7171
group.addoption('--mpl-baseline-path',
7272
help="directory containing baseline images, relative to location where py.test is run", action='store')
7373

74+
results_path_help = "directory for test results, relative to location where py.test is run"
75+
group.addoption('--mpl-results-path', help=results_path_help, action='store')
76+
parser.addini('mpl-results-path', help=results_path_help)
77+
7478

7579
def pytest_configure(config):
7680

7781
if config.getoption("--mpl") or config.getoption("--mpl-generate-path") is not None:
7882

7983
baseline_dir = config.getoption("--mpl-baseline-path")
8084
generate_dir = config.getoption("--mpl-generate-path")
85+
results_dir = config.getoption("--mpl-results-path") or config.getini("mpl-results-path")
8186

82-
if baseline_dir is not None and generate_dir is not None:
83-
warnings.warn("Ignoring --mpl-baseline-path since --mpl-generate-path is set")
87+
if generate_dir is not None:
88+
if baseline_dir is not None:
89+
warnings.warn("Ignoring --mpl-baseline-path since --mpl-generate-path is set")
90+
if results_dir is not None and generate_dir is not None:
91+
warnings.warn("Ignoring --mpl-result-path since --mpl-generate-path is set")
8492

8593
if baseline_dir is not None:
8694
baseline_dir = os.path.abspath(baseline_dir)
8795
if generate_dir is not None:
8896
baseline_dir = os.path.abspath(generate_dir)
97+
if results_dir is not None:
98+
results_dir = os.path.abspath(results_dir)
8999

90100
config.pluginmanager.register(ImageComparison(config,
91101
baseline_dir=baseline_dir,
92-
generate_dir=generate_dir))
102+
generate_dir=generate_dir,
103+
results_dir=results_dir))
93104

94105

95106
class ImageComparison(object):
96107

97-
def __init__(self, config, baseline_dir=None, generate_dir=None):
108+
def __init__(self, config, baseline_dir=None, generate_dir=None, results_dir=None):
98109
self.config = config
99110
self.baseline_dir = baseline_dir
100111
self.generate_dir = generate_dir
112+
self.results_dir = results_dir
113+
if self.results_dir and not os.path.exists(self.results_dir):
114+
os.mkdir(self.results_dir)
101115

102116
def pytest_runtest_setup(self, item):
103117

@@ -155,7 +169,7 @@ def item_function_wrapper(*args, **kwargs):
155169
if self.generate_dir is None:
156170

157171
# Save the figure
158-
result_dir = tempfile.mkdtemp()
172+
result_dir = tempfile.mkdtemp(dir=self.results_dir)
159173
test_image = os.path.abspath(os.path.join(result_dir, filename))
160174

161175
fig.savefig(test_image, **savefig_kwargs)
@@ -167,11 +181,9 @@ def item_function_wrapper(*args, **kwargs):
167181
baseline_image_ref = os.path.abspath(os.path.join(os.path.dirname(item.fspath.strpath), baseline_dir, filename))
168182

169183
if not os.path.exists(baseline_image_ref):
170-
raise Exception("""Image file not found for comparison test
171-
Generated Image:
172-
\t{test}
173-
This is expected for new tests.""".format(
174-
test=test_image))
184+
pytest.fail("Image file not found for comparison test. "
185+
"(This is expected for new tests.)\nGenerated Image: "
186+
"\n\t{test}".format(test=test_image), pytrace=False)
175187

176188
# distutils may put the baseline images in non-accessible places,
177189
# copy to our tmpdir to be sure to keep them in case of failure
@@ -183,7 +195,7 @@ def item_function_wrapper(*args, **kwargs):
183195
if msg is None:
184196
shutil.rmtree(result_dir)
185197
else:
186-
raise Exception(msg)
198+
pytest.fail(msg, pytrace=False)
187199

188200
else:
189201

tests/test_pytest_mpl.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,35 @@ def test_fails(tmpdir):
8686
assert code == 0
8787

8888

89+
TEST_OUTPUT_DIR = """
90+
import pytest
91+
import matplotlib.pyplot as plt
92+
@pytest.mark.mpl_image_compare
93+
def test_output_dir():
94+
fig = plt.figure()
95+
ax = fig.add_subplot(1, 1, 1)
96+
ax.plot([1, 2, 3])
97+
return fig
98+
"""
99+
100+
101+
def test_output_dir(tmpdir):
102+
test_file = tmpdir.join('test.py').strpath
103+
with open(test_file, 'w') as f:
104+
f.write(TEST_OUTPUT_DIR)
105+
106+
# When we run the test, we should get output images where we specify
107+
output_dir = tmpdir.join('test_output_dir').strpath
108+
code = subprocess.call('py.test --mpl-results-path={0} --mpl {1}'.format(output_dir, test_file),
109+
shell=True)
110+
111+
assert code != 0
112+
assert os.path.exists(output_dir)
113+
114+
# Listdir() is to get the random name that the output for the one test is written into
115+
assert os.path.exists(os.path.join(output_dir, os.listdir(output_dir)[0], 'test_output_dir.png'))
116+
117+
89118
TEST_GENERATE = """
90119
import pytest
91120
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)