diff options
author | Nirbheek Chauhan <nirbheek@centricular.com> | 2020-09-10 13:10:40 +0530 |
---|---|---|
committer | Nirbheek Chauhan <nirbheek.chauhan@gmail.com> | 2020-09-11 23:27:15 +0000 |
commit | c7adeac137a41dcf7187a11259cdabd190ea9c47 (patch) | |
tree | cfe89bcb315693250ac77e17c8505238da1e91f2 /run_unittests.py | |
parent | 6053da7a002dafea988f5cecc11afab181c8d011 (diff) | |
download | meson-c7adeac137a41dcf7187a11259cdabd190ea9c47.zip meson-c7adeac137a41dcf7187a11259cdabd190ea9c47.tar.gz meson-c7adeac137a41dcf7187a11259cdabd190ea9c47.tar.bz2 |
unit tests: Don't use pytest when running single tests
On my machine this spawns 24 processes and then runs like the single
test I asked it to run. With this change, running a single test goes
from 7 seconds to less than a second.
Diffstat (limited to 'run_unittests.py')
-rwxr-xr-x | run_unittests.py | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/run_unittests.py b/run_unittests.py index b76e489..bace349 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -9008,18 +9008,26 @@ def convert_args(argv): pytest_args += ['-k', ' or '.join(test_list)] return pytest_args +def running_single_tests(argv, cases): + ''' + Check whether we only got arguments for running individual tests, not + entire testcases, and not all testcases (no test args). + ''' + got_test_arg = False + for arg in argv: + if arg.startswith('-'): + continue + for case in cases: + if not arg.startswith(case): + continue + if '.' not in arg: + # Got a testcase, done + return False + got_test_arg = True + return got_test_arg + def main(): unset_envs() - try: - import pytest # noqa: F401 - # Need pytest-xdist for `-n` arg - import xdist # noqa: F401 - pytest_args = ['-n', 'auto', './run_unittests.py'] - pytest_args += convert_args(sys.argv[1:]) - return subprocess.run(python_command + ['-m', 'pytest'] + pytest_args).returncode - except ImportError: - print('pytest-xdist not found, using unittest instead') - # All attempts at locating pytest failed, fall back to plain unittest. cases = ['InternalTests', 'DataTests', 'AllPlatformTests', 'FailureTests', 'PythonTests', 'NativeFileTests', 'RewriterTests', 'CrossFileTests', 'TAPParserTests', @@ -9027,6 +9035,19 @@ def main(): 'LinuxlikeTests', 'LinuxCrossArmTests', 'LinuxCrossMingwTests', 'WindowsTests', 'DarwinTests'] + # Don't use pytest-xdist when running single unit tests since it wastes + # time spawning a lot of processes to distribute tests to in that case. + if not running_single_tests(sys.argv, cases): + try: + import pytest # noqa: F401 + # Need pytest-xdist for `-n` arg + import xdist # noqa: F401 + pytest_args = ['-n', 'auto', './run_unittests.py'] + pytest_args += convert_args(sys.argv[1:]) + return subprocess.run(python_command + ['-m', 'pytest'] + pytest_args).returncode + except ImportError: + print('pytest-xdist not found, using unittest instead') + # Fallback to plain unittest. return unittest.main(defaultTest=cases, buffer=True) if __name__ == '__main__': |