From bd6a9245cd32064be694a338f8046fd610fe45fd Mon Sep 17 00:00:00 2001 From: Eleanor Boyd Date: Thu, 27 Jun 2024 09:21:06 -0700 Subject: [PATCH] Add parent directory to sys.path for unittest discovery and execution (#23712) fixes https://github.com/microsoft/vscode-python/issues/23392 --- python_files/unittestadapter/discovery.py | 5 +++++ python_files/unittestadapter/execution.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/python_files/unittestadapter/discovery.py b/python_files/unittestadapter/discovery.py index 53f803a6a114..58ab8ca1a651 100644 --- a/python_files/unittestadapter/discovery.py +++ b/python_files/unittestadapter/discovery.py @@ -57,6 +57,11 @@ def discover_tests( } """ cwd = os.path.abspath(start_dir) + if "/" in start_dir: # is a subdir + parent_dir = os.path.dirname(start_dir) + sys.path.insert(0, parent_dir) + else: + sys.path.insert(0, cwd) payload: DiscoveryPayloadDict = {"cwd": cwd, "status": "success", "tests": None} tests = None error: List[str] = [] diff --git a/python_files/unittestadapter/execution.py b/python_files/unittestadapter/execution.py index 5645241fb651..84cc10c4fb1f 100644 --- a/python_files/unittestadapter/execution.py +++ b/python_files/unittestadapter/execution.py @@ -195,6 +195,11 @@ def run_tests( locals: Optional[bool] = None, ) -> ExecutionPayloadDict: cwd = os.path.abspath(start_dir) + if "/" in start_dir: # is a subdir + parent_dir = os.path.dirname(start_dir) + sys.path.insert(0, parent_dir) + else: + sys.path.insert(0, cwd) status = TestExecutionStatus.error error = None payload: ExecutionPayloadDict = {"cwd": cwd, "status": status, "result": None}