Skip to content

Instantly share code, notes, and snippets.

@craiga
Last active March 24, 2019 12:39
Show Gist options
  • Save craiga/08fef45a6bfac1bdce79778d599fac99 to your computer and use it in GitHub Desktop.
Save craiga/08fef45a6bfac1bdce79778d599fac99 to your computer and use it in GitHub Desktop.

Minimal reproduction of pytest-dev/pytest-mock#137.

$ pipenv install
…
$ pipenv run pytest
========================================= test session starts =========================================
platform darwin -- Python 3.7.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
rootdir: /Users/craiga/08fef45a6bfac1bdce79778d599fac99, inifile:
plugins: mock-1.10.1
collected 1 item                                                                                      

thing_to_be_tested_test.py .                                                                    [100%]

====================================== 1 passed in 0.01 seconds =======================================
Traceback (most recent call last):
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/bin/pytest", line 10, in <module>
    sys.exit(main())
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/_pytest/config/__init__.py", line 79, in main
    return config.hook.pytest_cmdline_main(config=config)
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/pluggy/manager.py", line 68, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/pluggy/manager.py", line 62, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/_pytest/main.py", line 242, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/_pytest/main.py", line 237, in wrap_session
    config._ensure_unconfigure()
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/_pytest/config/__init__.py", line 647, in _ensure_unconfigure
    fin()
  File "/Users/craiga/.ve/08fef45a6bfac1bdce79778d599fac99-CJE4LNxu/lib/python3.7/site-packages/pytest_mock.py", line 291, in unwrap_assert_methods
    patcher.stop()
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1403, in stop
    return self.__exit__()
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1367, in __exit__
    raise RuntimeError('stop called on unstarted patcher')
RuntimeError: stop called on unstarted patcher
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
pytest = "*"
pytest-mock = "*"
black = "==19.3b0"
[requires]
python_version = "3.7"
import random
def get_random_number():
return random.randint(0, 100)
from unittest import TestCase
from unittest.mock import patch
import thing_to_be_tested
class GetRandomNumberTestCase(TestCase):
def test_get_random_number(self):
patcher = patch("random.randint", lambda x, y: 5)
patcher.start()
self.assertEqual(5, thing_to_be_tested.get_random_number())
patch.stopall()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment