Skip to content

Commit f1c2ac2

Browse files
committed
add functions for running scripts to conftest
1 parent 4d70e15 commit f1c2ac2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import importlib.util
12
import json
23
from pathlib import Path
34

5+
import matplotlib
46
import pytest
57

8+
__examples_dir__ = Path(__file__).parent.parent / "docs" / "examples"
9+
610

711
@pytest.fixture
812
def user_filesystem(tmp_path):
@@ -17,3 +21,24 @@ def user_filesystem(tmp_path):
1721
json.dump(home_config_data, f)
1822

1923
yield tmp_path
24+
25+
26+
@pytest.fixture(scope="session", autouse=True)
27+
def use_headless_matplotlib():
28+
"""Force matplotlib to use a headless backend during tests."""
29+
matplotlib.use("Agg")
30+
31+
32+
def load_module_from_path(path: Path):
33+
"""Load a module given an absolute Path."""
34+
spec = importlib.util.spec_from_file_location(path.stem, path)
35+
module = importlib.util.module_from_spec(spec)
36+
spec.loader.exec_module(module)
37+
return module
38+
39+
40+
def run_cmi_script(script_path: Path):
41+
"""General runner for example scripts with a main()."""
42+
module = load_module_from_path(script_path)
43+
assert hasattr(module, "main"), f"{script_path} has no main() function"
44+
module.main()

0 commit comments

Comments
 (0)