Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jahan01 opened this issue Mar 20, 2024 · 5 comments · May be fixed by #23245
Open

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

jahan01 opened this issue Mar 20, 2024 · 5 comments · May be fixed by #23245
Assignees
Labels
area-testing info-needed Issue requires more information from poster

Comments

@jahan01
Copy link

jahan01 commented Mar 20, 2024

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:

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Mar 20, 2024
@anthonykim1 anthonykim1 added area-testing and removed triage-needed Needs assignment to the proper sub-team labels Mar 20, 2024
@eleanorjboyd
Copy link
Member

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.

@eleanorjboyd eleanorjboyd added the info-needed Issue requires more information from poster label Mar 27, 2024
@karthiknadig
Copy link
Member

@eleanorjboyd We just need to capture CWD on import, that should address this.

@eleanorjboyd
Copy link
Member

@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

@eleanorjboyd eleanorjboyd linked a pull request Apr 16, 2024 that will close this issue
@jahan01
Copy link
Author

jahan01 commented May 6, 2024

@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 ?

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

Make: Apple M2 Pro
OS: Sonoma 14.4.1

@eleanorjboyd
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-testing info-needed Issue requires more information from poster
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants