Skip to content

Commit 016034b

Browse files
committed
Add a script for running the Python test suites and include it in scripts/tests.sh
1 parent 326b814 commit 016034b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

scripts/tests.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ else
6262
log_directory=""
6363
fi
6464

65+
printTask "Testing Python scripts..."
66+
"$REPO_ROOT/test/pyscriptTests.py"
67+
6568
printTask "Running commandline tests..."
6669
# Only run in parallel if this is run on CI infrastructure
6770
if [[ -n "$CI" ]]

test/pyscriptTests.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
# Runs all tests for Python-based scripts.
4+
# Any Python test suites located in test/scripts/ will executed automatically by this script.
5+
6+
import sys
7+
from pathlib import Path
8+
from unittest import TestLoader, TextTestRunner
9+
10+
SCRIPTS_DIR = Path(__file__).parent.parent / "scripts"
11+
TEST_DIR = Path(__file__).parent.parent / "test/scripts/"
12+
13+
14+
if __name__ == '__main__':
15+
# Add the directory containing scripts to be tested to PYTHONPATH. This means that these
16+
# scripts can be imported from anywhere (in particular from the test suites) as if they were
17+
# installed globally. This is necessary because scripts and their tests are in separate
18+
# directories not recognized by Python as a part of the same package (i.e. their common parent
19+
# directory does not have an __init__.py file).
20+
# NOTE: This does not play well with relative imports from test suites so the suites must be
21+
# placed directly in TEST_DIR and not in its subdirectories. Relative imports from scripts
22+
# themselves work fine though.
23+
sys.path.insert(0, str(SCRIPTS_DIR))
24+
25+
# This is equivalent to `python -m unittest discover --start-directory $TEST_DIR`
26+
test_suite = TestLoader().discover(start_dir=TEST_DIR)
27+
result = TextTestRunner().run(test_suite)
28+
29+
if len(result.errors) > 0 or len(result.failures) > 0:
30+
sys.exit(1)

0 commit comments

Comments
 (0)