Repository navigation
Running tests via test explorer breaks when getcwd is mocked #23101
Copy link
Copy link
Closed as not planned
Labels
area-testinginfo-neededIssue requires more information from posterIssue requires more information from poster
Description
Type: Bug
Behaviour
Expected vs. Actual
In case where os.getcwd() is mocked / patched in the test case, executing it via test explorer throws exception.
Running the same test case via terminal works fine. Also works fine when run with pycharm's test explorer as well
Steps to reproduce:
- Have a test case where os.getcwd is mocked.
from unittest.mock import patch
import pytest
import os
@pytest.fixture
def other_error():
with patch.object(os, os.getcwd.__name__) as mock:
mock.side_effect = Exception("exception reading current dir")
yield mock
def test_cwd(other_error):
with pytest.raises(Exception):
os.getcwd()-
Configure test framework as pytest.
-
Run test via test explorer UI. You ll see below exception
Traceback (most recent call last):
File "/Users/myuser/.vscode/extensions/ms-python.python-2024.2.1/pythonFiles/vscode_pytest/run_pytest_script.py", line 70, in <module>
pytest.main(arg_array)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/config/__init__.py", line 167, in main
ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_hooks.py", line 493, in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_manager.py", line 115, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 113, in _multicall
raise exception.with_traceback(exception.__traceback__)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 77, in _multicall
res = hook_impl.function(*args)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/main.py", line 317, in pytest_cmdline_main
return wrap_session(config, _main)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/main.py", line 305, in wrap_session
config.hook.pytest_sessionfinish(
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_hooks.py", line 493, in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_manager.py", line 115, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 130, in _multicall
teardown[0].send(outcome)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/terminal.py", line 808, in pytest_sessionfinish
outcome.get_result()
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_result.py", line 114, in get_result
raise exc.with_traceback(exc.__traceback__)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 77, in _multicall
res = hook_impl.function(*args)
File "/Users/myuser/.vscode/extensions/ms-python.python-2024.2.1/pythonFiles/vscode_pytest/__init__.py", line 350, in pytest_sessionfinish
cwd = pathlib.Path.cwd()
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/pathlib.py", line 1096, in cwd
return cls(os.getcwd())
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/unittest/mock.py", line 1081, in __call__
return self._mock_call(*args, **kwargs)
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/unittest/mock.py", line 1085, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/unittest/mock.py", line 1140, in _execute_mock_call
raise effect
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/main.py", line 270, in wrap_session
session.exitstatus = doit(config, session) or 0
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/main.py", line 324, in _main
config.hook.pytest_runtestloop(session=session)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_hooks.py", line 493, in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_manager.py", line 115, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 152, in _multicall
return outcome.get_result()
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_result.py", line 114, in get_result
raise exc.with_traceback(exc.__traceback__)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 77, in _multicall
res = hook_impl.function(*args)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/main.py", line 349, in pytest_runtestloop
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_hooks.py", line 493, in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_manager.py", line 115, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 152, in _multicall
return outcome.get_result()
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_result.py", line 114, in get_result
raise exc.with_traceback(exc.__traceback__)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 77, in _multicall
res = hook_impl.function(*args)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/runner.py", line 112, in pytest_runtest_protocol
runtestprotocol(item, nextitem=nextitem)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/runner.py", line 125, in runtestprotocol
rep = call_and_report(item, "setup", log)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/runner.py", line 224, in call_and_report
hook.pytest_runtest_logreport(report=report)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_hooks.py", line 493, in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_manager.py", line 115, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 113, in _multicall
raise exception.with_traceback(exception.__traceback__)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 77, in _multicall
res = hook_impl.function(*args)
File "..../virtualenvs/venv/lib/python3.8/site-packages/_pytest/terminal.py", line 524, in pytest_runtest_logreport
] = self.config.hook.pytest_report_teststatus(report=rep, config=self.config)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_hooks.py", line 493, in __call__
return self._hookexec(self.name, self._hookimpls, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_manager.py", line 115, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 152, in _multicall
return outcome.get_result()
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_result.py", line 114, in get_result
raise exc.with_traceback(exc.__traceback__)
File "..../virtualenvs/venv/lib/python3.8/site-packages/pluggy/_callers.py", line 62, in _multicall
next(wrapper_gen) # first yield
File "/Users/myuser/.vscode/extensions/ms-python.python-2024.2.1/pythonFiles/vscode_pytest/__init__.py", line 227, in pytest_report_teststatus
cwd = pathlib.Path.cwd()
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/pathlib.py", line 1096, in cwd
return cls(os.getcwd())
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/unittest/mock.py", line 1081, in __call__
return self._mock_call(*args, **kwargs)
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/unittest/mock.py", line 1085, in _mock_call
return self._execute_mock_call(*args, **kwargs)
File "/Users/myuser/.pyenv/versions/3.8.18amd64/lib/python3.8/unittest/mock.py", line 1140, in _execute_mock_call
raise effect
Exception: exception reading current dir
Finished running tests!Diagnostic data
- Python version (& distribution if applicable, e.g. Anaconda): 3.8.18
- Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Poetry
- Value of the
python.languageServersetting: Pylance
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
XXX
User Settings
condaPath: "<placeholder>"
languageServer: "Pylance"
testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true
experiments
• optInto: ["pythonTerminalEnvVarActivation","pythonTestAdapter"]
Extension version: 2024.2.1
VS Code version: Code 1.87.2 (Universal) (863d2581ecda6849923a2118d93a088b0745d9d6, 2024-03-08T15:21:31.043Z)
OS version: Darwin arm64 23.3.0
Modes:
Metadata
Metadata
Assignees
Labels
area-testinginfo-neededIssue requires more information from posterIssue requires more information from poster
Activity
eleanorjboyd commentedon Mar 27, 2024
In our code we use the cwd to add to solve the problem outlined in this PR: #21399. Would using pathlib cwd as a fallback work? We need this information to make the extension work so we would have to get it somehow.
karthiknadig commentedon Apr 2, 2024
Eleanor Boyd (@eleanorjboyd) We just need to capture CWD on import, that should address this.
eleanorjboyd commentedon Apr 16, 2024
jahan01 two things,
can you try you example again and tell me if it is still failing? If so, what machine type are you using?
I have created a PR and it should fix the issue (as it is similar to one we had with mocking environment variables). But I wanted to confirm first before putting up the PR as I was not able to repro the issue
jahan01 commentedon May 6, 2024
Eleanor Boyd (@eleanorjboyd): Yes it is still failing with the latest vscode (Version: 1.89.0 (Universal)) and python extension (v2024.6.0). I given the machine info already in description. Do you require more info than what is given ?
Make: Apple M2 Pro
OS: Sonoma 14.4.1
eleanorjboyd commentedon May 6, 2024
Hi, sorry to clarify I have created the following workspace based on your example and am getting the test to pass. Does this fail on your machine? If so can you update versions of the extension / vscode and try again. If it doesn't break let me know what about my configuration of my workspace differs from yours
23101-mock-cwd.zip
eleanorjboyd commentedon May 31, 2024
closing due to inactivity- please comment and I can reopen if you are able to follow up on my above message. Thanks