Skip to content

Commit 8cfd82f

Browse files
authored
mach: Allow filtering tests in mach test-devtools (servo#38514)
`mach test-devtools` uses [unittest](https://docs.python.org/3/library/unittest.html) without [unittest.main()](https://docs.python.org/3/library/unittest.html#unittest.main), so we can parse arguments and set up the test runner how we want, but this means there’s currently no way to filter tests on the command line. this patch plumbs the positional arguments (if any) into the TestLoader’s [testNamePatterns](https://docs.python.org/3/library/unittest.html#unittest.TestLoader.testNamePatterns), with extra logic to treat any pattern `pattern` not containing `*` like `*pattern*`. this is the same behaviour as unittest.main() [`-k`](https://docs.python.org/3/library/unittest.html#cmdoption-unittest-k): ``` $ ./mach test-devtools domparser responsexml Running devtools tests... Running 2 tests: - test_source_content_inline_script_with_domparser (servo.devtools_tests.DevtoolsTests.test_source_content_inline_script_with_domparser) - test_source_content_inline_script_with_responsexml (servo.devtools_tests.DevtoolsTests.test_source_content_inline_script_with_responsexml) test_source_content_inline_script_with_domparser (servo.devtools_tests.DevtoolsTests.test_source_content_inline_script_with_domparser) ... ok test_source_content_inline_script_with_responsexml (servo.devtools_tests.DevtoolsTests.test_source_content_inline_script_with_responsexml) ... ok ---------------------------------------------------------------------- Ran 2 tests in 4.055s OK ``` Testing: not really worth automated testing, but tested manually above Fixes: part of servo#36325 --------- Signed-off-by: Delan Azabani <[email protected]>
1 parent 52ba8fa commit 8cfd82f

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

python/servo/devtools_tests.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,23 @@ def get_test_path(self, path: str) -> str:
317317
return os.path.join(DevtoolsTests.script_path, os.path.join("devtools_tests", path))
318318

319319

320-
def run_tests(script_path, build_type: BuildType):
320+
def run_tests(script_path, build_type: BuildType, test_names: list[str]):
321321
DevtoolsTests.script_path = script_path
322322
DevtoolsTests.build_type = build_type
323323
verbosity = 1 if logging.getLogger().level >= logging.WARN else 2
324-
suite = unittest.TestLoader().loadTestsFromTestCase(DevtoolsTests)
324+
loader = unittest.TestLoader()
325+
if test_names:
326+
patterns = []
327+
# unittest.main() `-k` treats any `pattern` not containing `*` like `*pattern*`
328+
for pattern in test_names:
329+
if "*" in pattern:
330+
patterns.append(pattern)
331+
else:
332+
patterns.append(f"*{pattern}*")
333+
loader.testNamePatterns = patterns
334+
suite = loader.loadTestsFromTestCase(DevtoolsTests)
335+
print(f"Running {suite.countTestCases()} tests:")
336+
for test in suite:
337+
print(f"- {test}")
338+
print()
325339
return unittest.TextTestRunner(verbosity=verbosity).run(suite).wasSuccessful()

python/servo/testing_commands.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,11 @@ def test_scripts(self, verbose, very_verbose, all, tests) -> int:
355355
return 0 if passed else 1
356356

357357
@Command("test-devtools", description="Run tests for devtools.", category="testing")
358+
@CommandArgument("test_names", nargs=argparse.REMAINDER, help="Only run tests that match these patterns")
358359
@CommandBase.common_command_arguments(build_type=True)
359-
def test_devtools(self, build_type: BuildType, **kwargs) -> int:
360+
def test_devtools(self, build_type: BuildType, test_names: list[str], **kwargs) -> int:
360361
print("Running devtools tests...")
361-
passed = servo.devtools_tests.run_tests(SCRIPT_PATH, build_type)
362+
passed = servo.devtools_tests.run_tests(SCRIPT_PATH, build_type, test_names)
362363
return 0 if passed else 1
363364

364365
@Command(

0 commit comments

Comments
 (0)