monkeypatch.replace() now only accepts a string. Improved error handling and

docs thanks to suggestions from flub, pelme, schmir, ronny.
This commit is contained in:
holger krekel
2013-08-07 16:49:29 +02:00
parent 407283ef81
commit 4b88d6d2d7
4 changed files with 72 additions and 86 deletions
+18 -1
View File
@@ -29,7 +29,7 @@ patch this function before calling into a function which uses it::
def test_mytest(monkeypatch):
def mockreturn(path):
return '/abc'
monkeypatch.setattr(os.path., 'expanduser', mockreturn)
monkeypatch.setattr(os.path, 'expanduser', mockreturn)
x = getssh()
assert x == '/abc/.ssh'
@@ -37,6 +37,23 @@ Here our test function monkeypatches ``os.path.expanduser`` and
then calls into an function that calls it. After the test function
finishes the ``os.path.expanduser`` modification will be undone.
example: preventing "requests" from remote operations
------------------------------------------------------
If you want to prevent the "requests" library from performing http
requests in all your tests, you can do::
# content of conftest.py
import pytest
@pytest.fixture(autouse=True)
def no_requests(monkeypatch):
monkeypatch.replace("requests.session.Session.request", None)
This autouse fixture will be executed for all test functions and it
will replace the method ``request.session.Session.request`` with the
value None so that any attempts to create http requests will fail.
Method reference of the monkeypatch function argument
-----------------------------------------------------