Skip to content

Commit f736ee6

Browse files
authored
Feature/v3/feature/test examples dependent (#390)
* Update example test to run dependent examples in order * Update example test to run dependent examples in order * Update CHANGELOG.md * Improve test directory handling * Improve test directory handling * Typo
1 parent 7ac41c2 commit f736ee6

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,12 @@ This replaces `specific_share_to_other_effects_*` parameters and inverts the dir
160160
- `fit_effects_to_model_coords()` method for effect data processing
161161
- `connect_and_transform()` method replacing several operations
162162
- **Testing improvements**: Eliminated warnings during test execution
163-
- Updated deprecated code patterns in tests and examples (e.g., `sink`/`source` → `inputs`/`outputs`, `'H'` → `'h'` frequency)
164-
- Refactored plotting logic to handle test environments explicitly with non-interactive backends
165-
- Added comprehensive warning filters in `__init__.py` and `pyproject.toml` to suppress third-party library warnings
166-
- Improved test fixtures with proper figure cleanup to prevent memory leaks
167-
- Enhanced backend detection and handling in `plotting.py` for both Matplotlib and Plotly
163+
- Updated deprecated code patterns in tests and examples (e.g., `sink`/`source` → `inputs`/`outputs`, `'H'` → `'h'` frequency)
164+
- Refactored plotting logic to handle test environments explicitly with non-interactive backends
165+
- Added comprehensive warning filters in `__init__.py` and `pyproject.toml` to suppress third-party library warnings
166+
- Improved test fixtures with proper figure cleanup to prevent memory leaks
167+
- Enhanced backend detection and handling in `plotting.py` for both Matplotlib and Plotly
168+
- Always run dependent test in order
168169
169170
170171
Until here -->

tests/test_examples.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,77 @@
88
# Path to the examples directory
99
EXAMPLES_DIR = Path(__file__).parent.parent / 'examples'
1010

11+
# Examples that have dependencies and must run in sequence
12+
DEPENDENT_EXAMPLES = (
13+
'02_Complex/complex_example.py',
14+
'02_Complex/complex_example_results.py',
15+
)
16+
1117

1218
@pytest.mark.parametrize(
1319
'example_script',
1420
sorted(
15-
EXAMPLES_DIR.rglob('*.py'), key=lambda path: (str(path.parent), path.name)
16-
), # Sort by parent and script name
17-
ids=lambda path: str(path.relative_to(EXAMPLES_DIR)), # Show relative file paths
21+
[p for p in EXAMPLES_DIR.rglob('*.py') if str(p.relative_to(EXAMPLES_DIR)) not in DEPENDENT_EXAMPLES],
22+
key=lambda path: (str(path.parent), path.name),
23+
),
24+
ids=lambda path: str(path.relative_to(EXAMPLES_DIR)),
1825
)
1926
@pytest.mark.examples
20-
def test_example_scripts(example_script):
27+
def test_independent_examples(example_script):
2128
"""
22-
Test all example scripts in the examples directory.
29+
Test independent example scripts.
2330
Ensures they run without errors.
2431
Changes the current working directory to the directory of the example script.
2532
Runs them alphabetically.
26-
This imitates behaviour of running the script directly
33+
This imitates behaviour of running the script directly.
2734
"""
2835
script_dir = example_script.parent
2936
original_cwd = os.getcwd()
3037

3138
try:
32-
# Change the working directory to the script's location
3339
os.chdir(script_dir)
3440

35-
# Run the script
3641
result = subprocess.run(
3742
[sys.executable, example_script.name],
3843
capture_output=True,
3944
text=True,
45+
timeout=180,
4046
)
41-
assert result.returncode == 0, f'Script {example_script} failed:\n{result.stderr}'
47+
assert result.returncode == 0, (
48+
f'Script {example_script} failed:\nSTDERR:\n{result.stderr}\nSTDOUT:\n{result.stdout}'
49+
)
50+
51+
except subprocess.TimeoutExpired:
52+
pytest.fail(f'Script {example_script} timed out after 180 seconds')
53+
54+
finally:
55+
os.chdir(original_cwd)
56+
57+
58+
@pytest.mark.examples
59+
def test_dependent_examples():
60+
"""Test examples that must run in order (complex_example.py generates data for complex_example_results.py)."""
61+
original_cwd = os.getcwd()
62+
63+
try:
64+
for script_path in DEPENDENT_EXAMPLES:
65+
script_full_path = EXAMPLES_DIR / script_path
66+
os.chdir(script_full_path.parent)
67+
68+
result = subprocess.run(
69+
[sys.executable, script_full_path.name],
70+
capture_output=True,
71+
text=True,
72+
timeout=180,
73+
)
74+
assert result.returncode == 0, f'{script_path} failed:\nSTDERR:\n{result.stderr}\nSTDOUT:\n{result.stdout}'
75+
76+
except subprocess.TimeoutExpired:
77+
pytest.fail(f'Script {script_path} timed out after 180 seconds')
4278

4379
finally:
44-
# Restore the original working directory
4580
os.chdir(original_cwd)
4681

4782

4883
if __name__ == '__main__':
49-
pytest.main(['-v', '--disable-warnings'])
84+
pytest.main(['-v', '--disable-warnings', '-m', 'examples'])

0 commit comments

Comments
 (0)