Skip to content

Commit

Permalink
Disable monkey patching "mock" when --tb is set to native
Browse files Browse the repository at this point in the history
Fix #44
  • Loading branch information
nicoddemus committed May 25, 2016
1 parent a6993b7 commit dfd2603
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
``repr`` string as well as related assertion failure messages.
Thanks `@jurko-gospodnetic`_ for the PR (`#40`_).

* Monkey patching is automatically disabled with the ``--tb=native`` option. The underlying
mechanism used to suppress traceback entries from ``mock`` module does not work with that option
anyway plus it generates confusing messages on Python 3.5 due to exception chaining (`#44`_).
Thanks `@blueyed`_ for the report.

.. _@jurko-gospodnetic: https://github.com/jurko-gospodnetic
.. _@asfaltboy: https://github.com/asfaltboy
.. _#40: https://github.com/pytest-dev/pytest-mock/issues/40
.. _#36: https://github.com/pytest-dev/pytest-mock/issues/36
.. _#40: https://github.com/pytest-dev/pytest-mock/issues/40
.. _#44: https://github.com/pytest-dev/pytest-mock/issues/44

1.0
---
Expand Down
3 changes: 2 additions & 1 deletion pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,6 @@ def parse_ini_boolean(value):


def pytest_configure(config):
if parse_ini_boolean(config.getini('mock_traceback_monkeypatch')):
tb = config.getoption('--tb')
if parse_ini_boolean(config.getini('mock_traceback_monkeypatch')) and tb != 'native':
wrap_assert_methods(config)
20 changes: 19 additions & 1 deletion test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def assert_argument_introspection(left, right):
raise AssertionError("DID NOT RAISE")


@pytest.mark.skipif(sys.version_info[0] == 3 and sys.version_info[1] in (3, 4),
@pytest.mark.skipif(sys.version_info[:2] in [(3, 3), (3, 4)],
reason="assert_not_called not available in python 3.3 and 3.4")
def test_assert_not_called_wrapper(mocker):
stub = mocker.stub()
Expand Down Expand Up @@ -489,3 +489,21 @@ def request(cls, method, args):
m = mocker.patch.object(Request, 'request')
Request.request(method='get', args={'type': 'application/json'})
m.assert_called_once_with(method='get', args={'type': 'application/json'})


def test_monkeypatch_native(testdir):
testdir.makepyfile("""
def test_foo(mocker):
stub = mocker.stub()
stub(1, greet='hello')
stub.assert_called_once_with(1, greet='hey')
""")
if hasattr(testdir, 'runpytest_subprocess'):
result = testdir.runpytest_subprocess('--tb=native')
else:
# pytest 2.7.X
result = testdir.runpytest('--tb=native')
assert result.ret == 1
assert 'Traceback (most recent call last)' in result.stdout.str()
assert 'During handling of the above exception' not in result.stdout.str()
assert 'Differing items:' not in result.stdout.str()

0 comments on commit dfd2603

Please sign in to comment.