11import os
22import subprocess
3+
34import nbformat
5+ import pytest
6+
7+
8+ _TEST_DIR = os .path .abspath (os .path .dirname (__file__ ))
9+ _EXCLUDE = ["Python_intro.ipynb" , "animate-landlab-output.ipynb" ]
10+
411
5- _TEST_DIR = os .path .dirname (__file__ )
12+ def all_notebooks ():
13+ notebooks = []
14+ for root , dirs , files in os .walk (_TEST_DIR ):
15+ for file in files :
16+ if (
17+ file .endswith (".ipynb" )
18+ and (file not in _EXCLUDE )
19+ and ("checkpoint" not in file )
20+ ):
21+ notebooks .append (os .path .join (root , file ))
22+ return notebooks
23+
24+
25+ def pytest_generate_tests (metafunc ):
26+ if "notebook" in metafunc .fixturenames :
27+ metafunc .parametrize ("notebook" , all_notebooks ())
628
7- _EXCLUDE = ['Python_intro.ipynb' ]
829
930def _notebook_run (path ):
1031 """Execute a notebook via nbconvert and collect output.
@@ -13,189 +34,39 @@ def _notebook_run(path):
1334 dirname , __ = os .path .split (path )
1435 os .chdir (dirname )
1536
16- in_fn = os .path .split (path )[- 1 ].split ('.' )[0 ]
17- temp_file_prefix = 'output_' + in_fn
18- temp_file_out = temp_file_prefix + '.ipynb'
19-
20- args = ["jupyter" , "nbconvert" , "--to" , "notebook" , "--execute" ,
21- "--ExecutePreprocessor.kernel_name=python" ,
22- "--ExecutePreprocessor.timeout=None" ,
23- "--output" , temp_file_prefix , path ]
37+ in_fn = os .path .split (path )[- 1 ].split ("." )[0 ]
38+ temp_file_prefix = "output_" + in_fn
39+ temp_file_out = temp_file_prefix + ".ipynb"
40+
41+ args = [
42+ "jupyter" ,
43+ "nbconvert" ,
44+ "--to" ,
45+ "notebook" ,
46+ "--execute" ,
47+ "--ExecutePreprocessor.kernel_name=python" ,
48+ "--ExecutePreprocessor.timeout=None" ,
49+ "--output" ,
50+ temp_file_prefix ,
51+ path ,
52+ ]
2453 subprocess .check_call (args )
2554
26- nb = nbformat .read (temp_file_out , nbformat .current_nbformat , encoding = ' UTF-8' )
55+ nb = nbformat .read (temp_file_out , nbformat .current_nbformat , encoding = " UTF-8" )
2756
28- errors = [output for cell in nb .cells if "outputs" in cell
29- for output in cell ["outputs" ]\
30- if output .output_type == "error" ]
57+ errors = [
58+ output
59+ for cell in nb .cells
60+ if "outputs" in cell
61+ for output in cell ["outputs" ]
62+ if output .output_type == "error"
63+ ]
3164 os .remove (temp_file_out )
3265
3366 return nb , errors
3467
3568
36- def test_tutorial_template ():
37- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "tutorial_template.ipynb" ))
38- assert errors == []
39-
40-
41- def test_gradient_and_divergence ():
42- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "gradient_and_divergence/gradient_and_divergence.ipynb" ))
43- assert errors == []
44-
45-
46- def test_component_tutorial ():
47- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "component_tutorial/component_tutorial.ipynb" ))
48- assert errors == []
49-
50-
51- def test_landlab_fault_scarp ():
52- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "fault_scarp/landlab-fault-scarp.ipynb" ))
53- assert errors == []
54-
55-
56- def test_TLHDiff_tutorial ():
57- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "transport-length_hillslope_diffuser/TLHDiff_tutorial.ipynb" ))
58- assert errors == []
59-
60-
61- def test_the_FlowDirectors ():
62- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "flow_direction_and_accumulation/the_FlowDirectors.ipynb" ))
63- assert errors == []
64-
65-
66- def test_compare_FlowDirectors ():
67- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "flow_direction_and_accumulation/compare_FlowDirectors.ipynb" ))
68- assert errors == []
69-
70-
71- def test_the_FlowAccumulator ():
72- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "flow_direction_and_accumulation/the_FlowAccumulator.ipynb" ))
73- assert errors == []
74-
75-
76- def test_overland_flow_driver ():
77- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "overland_flow/overland_flow_driver.ipynb" ))
78- assert errors == []
79-
80-
81- def test_making_components ():
82- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "making_components/making_components.ipynb" ))
83- assert errors == []
84-
85-
86- def test_landlab_plotting ():
87- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "plotting/landlab-plotting.ipynb" ))
88- assert errors == []
89-
90-
91- # this notebook not tested because it requires FFMPEG
92- # def test_animate_landlab_output():
93- # nb, errors = _notebook_run(os.path.join(_TEST_DIR, "plotting/animate-landlab-output.ipynb"))
94- # assert errors == []
95-
96-
97- def test_grid_object_demo ():
98- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "grid_object_demo/grid_object_demo.ipynb" ))
99- assert errors == []
100-
101-
102- def test_normal_fault_component_tutorial ():
103- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "normal_fault/normal_fault_component_tutorial.ipynb" ))
104- assert errors == []
105-
106-
107- def test_working_with_fields ():
108- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "fields/working_with_fields.ipynb" ))
109- assert errors == []
110-
111-
112- def test_mappers ():
113- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "mappers/mappers.ipynb" ))
114- assert errors == []
115-
116-
117- def test_flexure_1d ():
118- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "flexure/flexure_1d.ipynb" ))
119- assert errors == []
120-
121-
122- def test_lots_of_loads ():
123- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "flexure/lots_of_loads.ipynb" ))
124- assert errors == []
125-
126-
127- def test_set_BCs_on_raster_perimeter ():
128- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "boundary_conds/set_BCs_on_raster_perimeter.ipynb" ))
129- assert errors == []
130-
131-
132- def test_set_BCs_from_xy ():
133- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "boundary_conds/set_BCs_from_xy.ipynb" ))
134- assert errors == []
135-
136-
137- def test_set_watershed_BCs_raster ():
138- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "boundary_conds/set_watershed_BCs_raster.ipynb" ))
139- assert errors == []
140-
141-
142- def test_set_BCs_on_voronoi ():
143- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "boundary_conds/set_BCs_on_voronoi.ipynb" ))
144- assert errors == []
145-
146-
147- def test_lithology_and_litholayers (): # this doesn't exist on this branch yet
148- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "lithology/lithology_and_litholayers.ipynb" ))
149- assert errors == []
150-
151-
152- def test_reading_dem_into_landlab ():
153- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "reading_dem_into_landlab/reading_dem_into_landlab.ipynb" ))
154- assert errors == []
155-
156-
157- def test_application_of_flow__distance_utility ():
158- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "flow__distance_utility/application_of_flow__distance_utility.ipynb" ))
159- assert errors == []
160-
161-
162- def test_cellular_automaton_vegetation_flat_domain ():
163- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "ecohydrology/cellular_automaton_vegetation_flat_surface/cellular_automaton_vegetation_flat_domain.ipynb" ))
164- assert errors == []
165-
166-
167- def test_cellular_automaton_vegetation_DEM ():
168- nb , errors = _notebook_run (os .path .join (_TEST_DIR , "ecohydrology/cellular_automaton_vegetation_DEM/cellular_automaton_vegetation_DEM.ipynb" ))
169- assert errors == []
170-
171-
172- # def test_Python_intro():
173- # nb, errors = _notebook_run(os.path.join(_TEST_DIR, "python_intro/Python_intro.ipynb"))
174- # assert errors == []
175-
176-
177- # def test_all_ipynbs():
178- # failed = []
179- # to_run = []
180- #
181- # for root, dirs, files in os.walk(_TEST_DIR):
182- # for file in files:
183- # if (file.endswith('.ipynb') and (file not in _EXCLUDE) and ('checkpoint' not in file)):
184- # to_run.append(os.path.join(root, file))
185- # for fp in to_run:
186- # print('STARTING: '+os.path.split(fp)[-1])
187- # nb, errors = _notebook_run(os.path.join(fp))
188- # try:
189- # assert errors == []
190- # print('PASSED : '+file)
191- # except AssertionError:
192- # print('FAILED : '+file)
193- # print(errors)
194- # failed.append(file)
195- # except:
196- # failed.append(file)
197- #
198- # print(str(len(failed)) + ' notebooks failed')
199- # for fnb in failed:
200- # print(fnb)
201- # assert failed == []
69+ def test_notebook (tmpdir , notebook ):
70+ with tmpdir .as_cwd ():
71+ nb , errors = _notebook_run (notebook )
72+ assert not errors
0 commit comments