-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revamped test runner and made log time-stamps relative.
The preferred way to run the test suite is through `test.py` in the `deploy` folder. That script now also supports running individual test scripts (a.k.a. test "groups"), such as `test_node.py` by passing "node" as a command-line argument. Passing "--log" activates output of log messages (with debug-level granularity). The `import parent` statement in the test scripts was removed, as well as the corresponding helper module, which used to add the `mph` folder to `sys.path`. This means running test scripts directly via, for example, `python tests/test_node.py` from the root folder, will no longer work out of the box as the script will then fail to `import mph` (unless MPh has been installed separately via Pip or its folder been added manually to the PYTHONPATH environment variable). The setup of log output to the console during test runs was refactored. The test scripts now only accept one command-line option, `--log` just like the test runner, instead of `log` and `debug` before. Time-stamps are now relative with respect to the start of an individual test script, instead of the absolute wall-clock time they were previously.
- Loading branch information
Showing
19 changed files
with
145 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,93 @@ | ||
""" | ||
Runs all tests in the intended order. | ||
This script does not run the tests via pyTest, but just executes the | ||
test scripts as is, in a subprocess. (pyTest is however needed for some | ||
test fixtures, so it must be installed.) We impose the intended order, | ||
from the most basic functionality to the high-level abstractions. | ||
Pass 'log' as a command-line argument to have the scripts print log | ||
messages to the console while the tests are running. Pass 'debug' to | ||
increase the verbosity of the log. | ||
Each test script (in the `tests` folder) contains a group of tests. | ||
These scripts must be run in separate processes as most of them start | ||
and stop the Java virtual machine, which can only be done once per | ||
process. This is why simply calling pyTest (with `python -m pytest` | ||
in the root folder) will not work. | ||
This script here runs each test group in a new subprocess. It also | ||
imposes a logical order: from the tests covering the most most basic | ||
functionality to the high-level abstractions. | ||
Here, as opposed to the similar script `coverage.py`, we don't actually | ||
run the tests through pyTest. Rather, we run the scripts directly so | ||
that the output is less verbose. Note, however, that pyTest still needs | ||
to be installed as some of the test fixtures require it. | ||
The verbosity can be increased by passing `--log` as a command-line | ||
argument. This will display the log messages produced by MPh as the | ||
tests are running. You can also pass the name of a test group to run | ||
only that one. For example, passing "model" will only run the tests | ||
defined in `test_model.py`. | ||
""" | ||
|
||
from subprocess import run | ||
from pathlib import Path | ||
from timeit import default_timer as now | ||
import sys | ||
from argparse import ArgumentParser | ||
from sys import executable as python | ||
from sys import exit | ||
from os import environ, pathsep | ||
|
||
|
||
# Define order of test groups. | ||
groups = ['meta', 'config', 'discovery', 'server', 'session', 'standalone', | ||
'client', 'multi', 'node', 'model', 'exit'] | ||
|
||
# Determine path of project root folder. | ||
here = Path(__file__).resolve().parent | ||
root = here.parent | ||
|
||
# Run MPh in project folder, not a possibly different installed version. | ||
if 'PYTHONPATH' in environ: | ||
environ['PYTHONPATH'] = str(root) + pathsep + environ['PYTHONPATH'] | ||
else: | ||
environ['PYTHONPATH'] = str(root) | ||
|
||
tests = ['meta', 'config', 'discovery', 'server', 'session', 'standalone', | ||
'client', 'multi', 'node', 'model', 'exit'] | ||
# Parse command-line arguments. | ||
parser = ArgumentParser(prog='test.py', | ||
description='Runs the MPh test suite.', | ||
add_help=False, | ||
allow_abbrev=False) | ||
parser.add_argument('--help', | ||
help='Show this help message.', | ||
action='help') | ||
parser.add_argument('--log', | ||
help='Display log output.', | ||
action='store_true') | ||
parser.add_argument('--groups', | ||
help='List all test groups.', | ||
action='store_true') | ||
parser.add_argument('group', | ||
help='Run only this group of tests.', | ||
nargs='?') | ||
arguments = parser.parse_args() | ||
if arguments.groups: | ||
for group in groups: | ||
print(group) | ||
exit() | ||
if arguments.group: | ||
group = arguments.group | ||
if group.startswith('test_'): | ||
group = group[5:] | ||
if group.endswith('.py'): | ||
group = group[:-3] | ||
groups = [group] | ||
options = [] | ||
if arguments.log: | ||
options.append('--log') | ||
|
||
folder = Path(__file__).parent.parent / 'tests' | ||
python = sys.executable | ||
arguments = sys.argv[1:] | ||
for test in tests: | ||
print(f'test_{test}') | ||
# Run each test group in new process. | ||
for group in groups: | ||
if groups.index(group) > 0: | ||
print() | ||
print(f'Running test group "{group}".') | ||
t0 = now() | ||
process = run([python, f'test_{test}.py'] + arguments, cwd=folder) | ||
process = run([python, f'test_{group}.py'] + options, cwd=root/'tests') | ||
if process.returncode == 0: | ||
print(f'Passed in {now()-t0:.0f} s.') | ||
else: | ||
print(f'Failed after {now()-t0:.0f} s.') | ||
print() | ||
exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
"""Process exiting with exception when no Java VM is running.""" | ||
|
||
import parent # noqa F401 | ||
import mph # noqa F401 | ||
import mph # noqa F401 | ||
|
||
raise RuntimeError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
"""Process exiting via `sys.exit()` with no Java VM running.""" | ||
|
||
import parent # noqa F401 | ||
import mph # noqa F401 | ||
import mph # noqa F401 | ||
import sys | ||
|
||
sys.exit(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.