Skip to content

Running tests via test explorer breaks when getcwd is mocked #23101

Closed as not planned
@jahan01

Description

@jahan01

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:

  1. 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()
  1. Configure test framework as pytest.

  2. 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.languageServer setting: Pylance
Output for Python in the Output panel (ViewOutput, 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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions