Skip to content

Commit bd6a924

Browse files
authored
Add parent directory to sys.path for unittest discovery and execution (#23712)
fixes #23392
1 parent 62ded1f commit bd6a924

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

python_files/unittestadapter/discovery.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def discover_tests(
5757
}
5858
"""
5959
cwd = os.path.abspath(start_dir)
60+
if "/" in start_dir: # is a subdir
61+
parent_dir = os.path.dirname(start_dir)
62+
sys.path.insert(0, parent_dir)
63+
else:
64+
sys.path.insert(0, cwd)
6065
payload: DiscoveryPayloadDict = {"cwd": cwd, "status": "success", "tests": None}
6166
tests = None
6267
error: List[str] = []

python_files/unittestadapter/execution.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ def run_tests(
195195
locals: Optional[bool] = None,
196196
) -> ExecutionPayloadDict:
197197
cwd = os.path.abspath(start_dir)
198+
if "/" in start_dir: # is a subdir
199+
parent_dir = os.path.dirname(start_dir)
200+
sys.path.insert(0, parent_dir)
201+
else:
202+
sys.path.insert(0, cwd)
198203
status = TestExecutionStatus.error
199204
error = None
200205
payload: ExecutionPayloadDict = {"cwd": cwd, "status": status, "result": None}

0 commit comments

Comments
 (0)