From 6ba2b7f8c36e3f2945223183d99c043f0069a8c9 Mon Sep 17 00:00:00 2001 From: Silvris <58583688+Silvris@users.noreply.github.com> Date: Sun, 17 Aug 2025 19:46:48 -0500 Subject: [PATCH] Tests: implement pattern for filtering unittests locally (#5080) --- test/worlds/__init__.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/test/worlds/__init__.py b/test/worlds/__init__.py index 4bc01751..4a4e3e07 100644 --- a/test/worlds/__init__.py +++ b/test/worlds/__init__.py @@ -1,17 +1,46 @@ -def load_tests(loader, standard_tests, pattern): +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from unittest import TestLoader, TestSuite + + +def load_tests(loader: "TestLoader", standard_tests: "TestSuite", pattern: str): import os import unittest + import fnmatch from .. import file_path from worlds.AutoWorld import AutoWorldRegister suite = unittest.TestSuite() suite.addTests(standard_tests) + + # pattern hack + # all tests from within __init__ are always imported, so we need to filter out the folder earlier + # if the pattern isn't matching a specific world, we don't have much of a solution + + if pattern.startswith("worlds."): + if pattern.endswith(".py"): + pattern = pattern[:-3] + components = pattern.split(".") + world_glob = f"worlds.{components[1]}" + pattern = components[-1] + + elif pattern.startswith(f"worlds{os.path.sep}") or pattern.startswith(f"worlds{os.path.altsep}"): + components = pattern.split(os.path.sep) + if len(components) == 1: + components = pattern.split(os.path.altsep) + world_glob = f"worlds.{components[1]}" + pattern = components[-1] + else: + world_glob = "*" + + folders = [os.path.join(os.path.split(world.__file__)[0], "test") - for world in AutoWorldRegister.world_types.values()] + for world in AutoWorldRegister.world_types.values() + if fnmatch.fnmatch(world.__module__, world_glob)] all_tests = [ test_case for folder in folders if os.path.exists(folder) - for test_collection in loader.discover(folder, top_level_dir=file_path) + for test_collection in loader.discover(folder, top_level_dir=file_path, pattern=pattern) for test_suite in test_collection if isinstance(test_suite, unittest.suite.TestSuite) for test_case in test_suite ]