Skip to content

Commit 50ac49c

Browse files
andrewleechdpgeorge
authored andcommitted
unittest-discover: Avoid adding test parent dir to sys.path.
When running tests from subfolders, import by "full dotted path" rather than just module name, removing the need to add the test parent folder to `sys.path`. This matches CPython more closely, which places `abspath(top)` at the start of `sys.path` but doesn't include the test file parent dir at all. It fixes issues where projects may include a `test_xxx.py` file in their distribution which would (prior to this change) be unintentionally found by unittest-discover. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent 98f8a7e commit 50ac49c

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

python-stdlib/unittest-discover/manifest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
metadata(version="0.1.2")
1+
metadata(version="0.1.3")
22

33
require("argparse")
44
require("fnmatch")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
imported=True
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
importsys
2+
importunittest
3+
4+
5+
classTestModuleImport(unittest.TestCase):
6+
deftest_ModuleImportPath(self):
7+
try:
8+
fromsub.subimportimported
9+
assertimported
10+
exceptImportError:
11+
print("This test is intended to be run with unittest discover"
12+
"from the unittest-discover/tests dir. sys.path:", sys.path)
13+
raise

python-stdlib/unittest-discover/unittest/__main__.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
fromfnmatchimportfnmatch
77
frommicropythonimportconst
88

9-
fromunittestimportTestRunner, TestResult, TestSuite
9+
try:
10+
fromunittestimportTestRunner, TestResult, TestSuite
11+
exceptImportError:
12+
print("Error: This must be used from an installed copy of unittest-discover which will"
13+
" also install base unittest module.")
14+
raise
1015

1116

1217
# Run a single test in a clean environment.
1318
def_run_test_module(runner: TestRunner, module_name: str, *extra_paths: list[str]):
1419
module_snapshot= {k: vfork, vinsys.modules.items()}
1520
path_snapshot=sys.path[:]
1621
try:
17-
forpathinreversed(extra_paths):
22+
forpathinextra_paths:
1823
ifpath:
1924
sys.path.insert(0, path)
2025

21-
module=__import__(module_name)
26+
module=__import__(module_name, None, None, module_name)
2227
suite=TestSuite(module_name)
2328
suite._load_module(module)
2429
returnrunner.run(suite)
@@ -36,16 +41,18 @@ def _run_all_in_dir(runner: TestRunner, path: str, pattern: str, top: str):
3641
forfname, ftype, *_inos.ilistdir(path):
3742
iffnamein ("..", "."):
3843
continue
44+
fpath="/".join((path, fname))
3945
ifftype==_DIR_TYPE:
4046
result+=_run_all_in_dir(
4147
runner=runner,
42-
path="/".join((path, fname)),
48+
path=fpath,
4349
pattern=pattern,
4450
top=top,
4551
)
4652
iffnmatch(fname, pattern):
47-
module_name=fname.rsplit(".", 1)[0]
48-
result+=_run_test_module(runner, module_name, path, top)
53+
module_path=fpath.rsplit(".", 1)[0] # remove ext
54+
module_path=module_path.replace("/", ".").strip(".")
55+
result+=_run_test_module(runner, module_path, top)
4956
returnresult
5057

5158

0 commit comments

Comments
 (0)
close