| 924 | self.test_loader.testNamePatterns = original_test_name_patterns |
| 925 | |
| 926 | def load_tests_for_label(self, label, discover_kwargs): |
| 927 | label_as_path = os.path.abspath(label) |
| 928 | tests = None |
| 929 | |
| 930 | # If a module, or "module.ClassName[.method_name]", just run those. |
| 931 | if not os.path.exists(label_as_path): |
| 932 | with self.load_with_patterns(): |
| 933 | tests = self.test_loader.loadTestsFromName(label) |
| 934 | if tests.countTestCases(): |
| 935 | return tests |
| 936 | # Try discovery if "label" is a package or directory. |
| 937 | is_importable, is_package = try_importing(label) |
| 938 | if is_importable: |
| 939 | if not is_package: |
| 940 | return tests |
| 941 | elif not os.path.isdir(label_as_path): |
| 942 | if os.path.exists(label_as_path): |
| 943 | assert tests is None |
| 944 | raise RuntimeError( |
| 945 | f"One of the test labels is a path to a file: {label!r}, " |
| 946 | f"which is not supported. Use a dotted module name or " |
| 947 | f"path to a directory instead." |
| 948 | ) |
| 949 | return tests |
| 950 | |
| 951 | kwargs = discover_kwargs.copy() |
| 952 | if os.path.isdir(label_as_path) and not self.top_level: |
| 953 | kwargs["top_level_dir"] = find_top_level(label_as_path) |
| 954 | |
| 955 | with self.load_with_patterns(): |
| 956 | tests = self.test_loader.discover(start_dir=label, **kwargs) |
| 957 | |
| 958 | # Make unittest forget the top-level dir it calculated from this run, |
| 959 | # to support running tests from two different top-levels. |
| 960 | self.test_loader._top_level_dir = None |
| 961 | return tests |
| 962 | |
| 963 | def build_suite(self, test_labels=None, **kwargs): |
| 964 | test_labels = test_labels or ["."] |