|
8 | 8 | # Path to the examples directory |
9 | 9 | EXAMPLES_DIR = Path(__file__).parent.parent / 'examples' |
10 | 10 |
|
| 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 | + |
11 | 17 |
|
12 | 18 | @pytest.mark.parametrize( |
13 | 19 | 'example_script', |
14 | 20 | 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)), |
18 | 25 | ) |
19 | 26 | @pytest.mark.examples |
20 | | -def test_example_scripts(example_script): |
| 27 | +def test_independent_examples(example_script): |
21 | 28 | """ |
22 | | - Test all example scripts in the examples directory. |
| 29 | + Test independent example scripts. |
23 | 30 | Ensures they run without errors. |
24 | 31 | Changes the current working directory to the directory of the example script. |
25 | 32 | Runs them alphabetically. |
26 | | - This imitates behaviour of running the script directly |
| 33 | + This imitates behaviour of running the script directly. |
27 | 34 | """ |
28 | 35 | script_dir = example_script.parent |
29 | 36 | original_cwd = os.getcwd() |
30 | 37 |
|
31 | 38 | try: |
32 | | - # Change the working directory to the script's location |
33 | 39 | os.chdir(script_dir) |
34 | 40 |
|
35 | | - # Run the script |
36 | 41 | result = subprocess.run( |
37 | 42 | [sys.executable, example_script.name], |
38 | 43 | capture_output=True, |
39 | 44 | text=True, |
| 45 | + timeout=180, |
40 | 46 | ) |
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') |
42 | 78 |
|
43 | 79 | finally: |
44 | | - # Restore the original working directory |
45 | 80 | os.chdir(original_cwd) |
46 | 81 |
|
47 | 82 |
|
48 | 83 | if __name__ == '__main__': |
49 | | - pytest.main(['-v', '--disable-warnings']) |
| 84 | + pytest.main(['-v', '--disable-warnings', '-m', 'examples']) |
0 commit comments