patch.stopall() breaks pytest-mock
See original GitHub issueI’ve just added pytest
and pytest-mock
to a legacy project and found that this error was being raised after tests completed.
Traceback (most recent call last):
File "…/lib/python2.7/site-packages/pytest.py", line 89, in <module>
raise SystemExit(pytest.main())
File "…/lib/python2.7/site-packages/_pytest/config/__init__.py", line 80, in main
return config.hook.pytest_cmdline_main(config=config)
File "…/lib/python2.7/site-packages/pluggy/hooks.py", line 284, in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "…/lib/python2.7/site-packages/pluggy/manager.py", line 68, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "…/lib/python2.7/site-packages/pluggy/manager.py", line 62, in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "…/lib/python2.7/site-packages/pluggy/callers.py", line 208, in _multicall
return outcome.get_result()
File "…/lib/python2.7/site-packages/pluggy/callers.py", line 81, in get_result
_reraise(*ex) # noqa
File "…/lib/python2.7/site-packages/pluggy/callers.py", line 187, in _multicall
res = hook_impl.function(*args)
File "…/lib/python2.7/site-packages/_pytest/main.py", line 236, in pytest_cmdline_main
return wrap_session(config, _main)
File "…/lib/python2.7/site-packages/_pytest/main.py", line 231, in wrap_session
config._ensure_unconfigure()
File "…/lib/python2.7/site-packages/_pytest/config/__init__.py", line 648, in _ensure_unconfigure
fin()
File "…/lib/python2.7/site-packages/pytest_mock.py", line 291, in unwrap_assert_methods
patcher.stop()
File "…/lib/python2.7/site-packages/mock/mock.py", line 1513, in stop
return self.__exit__()
File "…/lib/python2.7/site-packages/mock/mock.py", line 1477, in __exit__
raise RuntimeError('stop called on unstarted patcher')
RuntimeError: stop called on unstarted patcher
One of the tests in the test suite included a call to patch.stopall()
, which seems to have stopped pytest-mock
’s mocks and caused this error.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
unittest.mock — mock object library — Python 3.11.1 ...
The patch() decorators makes it easy to temporarily replace classes in a particular module with a Mock object. By default patch() will create...
Read more >Mocks leaking into other test functions breaking assertions ...
I'm in the process of converting unit tests for my personal project from unittest -> pytest . Previously I was using the built-in...
Read more >Debian Bug report logs - #1006736 python-pytest-asyncio ...
Subject: python-pytest-asyncio breaks pytest-mock autopkgtest: ... assert get_random_number() == 5 mocker.mock_module.patch.stopall() ...
Read more >Right Way to Test, Mock, and Patch in Python - Medium
It is also possible to stop all patches which have been started (using start() ) by using patch.stopall() . The patch 's general...
Read more >pytest-mock 3.6.1 - PyPI
patch.dict · mocker.stopall. mocker.resetall(): calls reset_mock() in all mocked objects up to this point.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @nicoddemus and @blueyed for fixing it! 💖
@craiga See https://github.com/pytest-dev/pytest-mock/pull/138.