@@ -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
7579def 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
95106class 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.)\n Generated 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
0 commit comments