|
| 1 | +import os |
1 | 2 | import subprocess |
2 | 3 | import sys |
3 | 4 | import time |
4 | 5 | from pathlib import Path |
5 | 6 |
|
6 | 7 | import cv2 |
| 8 | +import pytest |
7 | 9 |
|
8 | | -EXAMPLES_DIR = Path(__file__).parent.parent.parent / "examples" |
| 10 | +EXAMPLES_DIR = Path(__file__).parents[1] / 'examples' |
9 | 11 |
|
| 12 | +# Create a temporary directory for the tests |
| 13 | +Path('/tmp/depthai_sdk_tests').mkdir(exist_ok=True) |
| 14 | +os.chdir('/tmp/depthai_sdk_tests') |
10 | 15 |
|
11 | | -def test_examples(): |
12 | | - python_executable = Path(sys.executable) |
13 | | - for example in EXAMPLES_DIR.rglob("**/*.py"): |
14 | | - print(f"Running example: {example.name}") |
15 | | - |
16 | | - result = subprocess.Popen(f"{python_executable} {example}", stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
17 | | - env={"DISPLAY": ""}, shell=True) |
18 | | - |
19 | | - time.sleep(5) |
20 | | - result.kill() |
21 | | - time.sleep(5) |
22 | | - print('Stderr: ', result.stderr.read().decode()) |
23 | 16 |
|
24 | | - # if result.returncode and result.returncode != 0: |
25 | | - # assert False, f"{example} raised an exception: {result.stderr}" |
26 | | - |
27 | | - cv2.destroyAllWindows() |
| 17 | +@pytest.mark.parametrize('example', list(EXAMPLES_DIR.rglob("**/*.py"))) |
| 18 | +def test_examples(example): |
| 19 | + print(f"Running {example}") |
| 20 | + python_executable = Path(sys.executable) |
| 21 | + result = subprocess.Popen(f"{python_executable} {example}", |
| 22 | + stdout=subprocess.PIPE, |
| 23 | + stderr=subprocess.PIPE, |
| 24 | + env={ |
| 25 | + 'DISPLAY': '', |
| 26 | + 'PYTHONPATH': f'{os.environ["PYTHONPATH"]}:{EXAMPLES_DIR.parent}' |
| 27 | + }, |
| 28 | + shell=True) |
| 29 | + |
| 30 | + time.sleep(5) |
| 31 | + result.kill() |
| 32 | + time.sleep(5) |
| 33 | + print('Stderr: ', result.stderr.read().decode()) |
| 34 | + |
| 35 | + if result.returncode and result.returncode != 0: |
| 36 | + assert False, f"{example} raised an exception: {result.stderr}" |
| 37 | + |
| 38 | + cv2.destroyAllWindows() |
0 commit comments