Skip to content

Commit 5b27183

Browse files
committed
glob files in examples dir
1 parent f1c2ac2 commit 5b27183

File tree

7 files changed

+50
-55
lines changed

7 files changed

+50
-55
lines changed

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ def load_module_from_path(path: Path):
4040
def run_cmi_script(script_path: Path):
4141
"""General runner for example scripts with a main()."""
4242
module = load_module_from_path(script_path)
43-
assert hasattr(module, "main"), f"{script_path} has no main() function"
43+
if not hasattr(module, "main"):
44+
pytest.skip(f"{script_path} has no main() function")
4445
module.main()

tests/test_ch03.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
import pytest
44
from conftest import __examples_dir__, run_cmi_script
55

6+
chapter = "ch03NiModelling"
7+
chapter_dir = Path(__examples_dir__) / chapter
8+
example_scripts = list(chapter_dir.rglob("*.py"))
69

7-
@pytest.mark.parametrize(
8-
"relative_path",
9-
[
10-
f"{__examples_dir__}/ch03NiModelling"
11-
+ "/solutions/diffpy-cmi/fitBulkNi.py",
12-
f"{__examples_dir__}/ch03NiModelling"
13-
+ "/solutions/diffpy-cmi/fitNPPt.py",
14-
],
15-
)
16-
def test_ch03_examples(relative_path):
17-
script_path = Path(__file__).parent.parent / relative_path
10+
11+
# Runs all example scripts in chapter 3, skips files in main() is not defined.
12+
# Passes if script runs without error.
13+
@pytest.mark.parametrize("script_path", example_scripts, ids=lambda p: p.name)
14+
def test_ch03_examples(script_path):
1815
run_cmi_script(script_path)

tests/test_ch05.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import pytest
44
from conftest import __examples_dir__, run_cmi_script
55

6+
chapter = "ch05Fit2Phase"
7+
chapter_dir = Path(__examples_dir__) / chapter
8+
example_scripts = list(chapter_dir.rglob("*.py"))
69

7-
@pytest.mark.parametrize(
8-
"relative_path",
9-
[
10-
f"{__examples_dir__}/ch05Fit2Phase/solutions/diffpy-cmi/fit2P.py",
11-
],
12-
)
13-
def test_ch05_examples(relative_path):
14-
script_path = Path(__file__).parent.parent / relative_path
10+
11+
# Runs all example scripts in chapter 5, skips files in main() is not defined.
12+
# Passes if script runs without error.
13+
@pytest.mark.parametrize("script_path", example_scripts, ids=lambda p: p.name)
14+
def test_ch03_examples(script_path):
1515
run_cmi_script(script_path)

tests/test_ch06.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import pytest
44
from conftest import __examples_dir__, run_cmi_script
55

6+
chapter = "ch06RefineCrystalStructureGen"
7+
chapter_dir = Path(__examples_dir__) / chapter
8+
example_scripts = list(chapter_dir.rglob("*.py"))
69

7-
@pytest.mark.parametrize(
8-
"relative_path",
9-
[
10-
f"{__examples_dir__}/ch06RefineCrystalStructureGen"
11-
+ "/solutions/diffpy-cmi/fitCrystalGen.py",
12-
],
13-
)
14-
def test_ch06_examples(relative_path):
15-
script_path = Path(__file__).parent.parent / relative_path
10+
11+
# Runs all example scripts in chapter 6, skips files in main() is not defined.
12+
# Passes if script runs without error.
13+
@pytest.mark.parametrize("script_path", example_scripts, ids=lambda p: p.name)
14+
def test_ch03_examples(script_path):
1615
run_cmi_script(script_path)

tests/test_ch07.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import pytest
44
from conftest import __examples_dir__, run_cmi_script
55

6+
chapter = "ch07StructuralPhaseTransitions"
7+
chapter_dir = Path(__examples_dir__) / chapter
8+
example_scripts = list(chapter_dir.rglob("*.py"))
69

7-
@pytest.mark.parametrize(
8-
"relative_path",
9-
[
10-
f"{__examples_dir__}/ch07StructuralPhaseTransitions"
11-
+ "/solutions/diffpy-cmi/fitTSeries.py",
12-
],
13-
)
14-
def test_ch07_examples(relative_path):
15-
script_path = Path(__file__).parent.parent / relative_path
10+
11+
# Runs all example scripts in chapter 7, skips files in main() is not defined.
12+
# Passes if script runs without error.
13+
@pytest.mark.parametrize("script_path", example_scripts, ids=lambda p: p.name)
14+
def test_ch03_examples(script_path):
1615
run_cmi_script(script_path)

tests/test_ch08.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
import pytest
44
from conftest import __examples_dir__, run_cmi_script
55

6+
chapter = "ch08NPRefinement"
7+
chapter_dir = Path(__examples_dir__) / chapter
8+
example_scripts = list(chapter_dir.rglob("*.py"))
69

7-
@pytest.mark.parametrize(
8-
"relative_path",
9-
[
10-
f"{__examples_dir__}/ch08NPRefinement"
11-
+ "/solutions/diffpy-cmi/fitCdSeNP.py",
12-
],
13-
)
14-
def test_ch08_examples(relative_path):
15-
script_path = Path(__file__).parent.parent / relative_path
10+
11+
# Runs all example scripts in chapter 8, skips files in main() is not defined.
12+
# Passes if script runs without error.
13+
@pytest.mark.parametrize("script_path", example_scripts, ids=lambda p: p.name)
14+
def test_ch03_examples(script_path):
1615
run_cmi_script(script_path)

tests/test_ch11.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import pytest
44
from conftest import __examples_dir__, run_cmi_script
55

6+
chapter = "ch11ClusterXYZ"
7+
chapter_dir = Path(__examples_dir__) / chapter
8+
example_scripts = list(chapter_dir.rglob("*.py"))
69

7-
@pytest.mark.parametrize(
8-
"relative_path",
9-
[
10-
f"{__examples_dir__}/ch11ClusterXYZ/solutions/diffpy-cmi/fitCdSeNP.py",
11-
],
12-
)
13-
def test_ch11_examples(relative_path):
14-
script_path = Path(__file__).parent.parent / relative_path
10+
11+
# Runs all example scripts in chapter 11, skips files in main() is not defined.
12+
# Passes if script runs without error.
13+
@pytest.mark.parametrize("script_path", example_scripts, ids=lambda p: p.name)
14+
def test_ch03_examples(script_path):
1515
run_cmi_script(script_path)

0 commit comments

Comments
 (0)