question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

patch.stopall() breaks pytest-mock

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
craigacommented, Mar 25, 2019

Thanks @nicoddemus and @blueyed for fixing it! 💖

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found