2010-10-11 05:45:45 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Monkeypatching/mocking modules and environments
|
2010-10-11 05:45:45 +08:00
|
|
|
================================================================
|
|
|
|
|
2010-11-13 18:10:45 +08:00
|
|
|
.. currentmodule:: _pytest.monkeypatch
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
Sometimes tests need to invoke functionality which depends
|
|
|
|
on global settings or which invokes code which cannot be easily
|
|
|
|
tested such as network access. The ``monkeypatch`` function argument
|
|
|
|
helps you to safely set/delete an attribute, dictionary item or
|
|
|
|
environment variable or to modify ``sys.path`` for importing.
|
2011-03-04 06:40:38 +08:00
|
|
|
See the `monkeypatch blog post`_ for some introduction material
|
|
|
|
and a discussion of its motivation.
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
.. _`monkeypatch blog post`: http://tetamap.wordpress.com/2009/03/03/monkeypatching-in-unit-tests-done-right/
|
|
|
|
|
2012-01-07 04:37:18 +08:00
|
|
|
Simple example: monkeypatching functions
|
2010-10-11 05:45:45 +08:00
|
|
|
---------------------------------------------------
|
|
|
|
|
2012-01-07 04:37:18 +08:00
|
|
|
If you want to pretend that ``os.expanduser`` returns a certain
|
2010-10-11 05:45:45 +08:00
|
|
|
directory, you can use the :py:meth:`monkeypatch.setattr` method to
|
|
|
|
patch this function before calling into a function which uses it::
|
|
|
|
|
2012-01-07 04:37:18 +08:00
|
|
|
# content of test_module.py
|
2010-10-11 05:45:45 +08:00
|
|
|
import os.path
|
|
|
|
def getssh(): # pseudo application code
|
2011-02-18 13:52:18 +08:00
|
|
|
return os.path.join(os.path.expanduser("~admin"), '.ssh')
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
def test_mytest(monkeypatch):
|
2010-10-11 16:07:14 +08:00
|
|
|
def mockreturn(path):
|
|
|
|
return '/abc'
|
2013-08-07 22:49:29 +08:00
|
|
|
monkeypatch.setattr(os.path, 'expanduser', mockreturn)
|
2010-10-11 05:45:45 +08:00
|
|
|
x = getssh()
|
2011-02-18 13:52:18 +08:00
|
|
|
assert x == '/abc/.ssh'
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2012-01-07 04:37:18 +08:00
|
|
|
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.
|
|
|
|
|
2013-08-07 22:49:29 +08:00
|
|
|
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):
|
2014-08-19 03:44:34 +08:00
|
|
|
monkeypatch.delattr("requests.sessions.Session.request")
|
2013-09-27 18:33:06 +08:00
|
|
|
|
|
|
|
This autouse fixture will be executed for each test function and it
|
|
|
|
will delete the method ``request.session.Session.request``
|
|
|
|
so that any attempts within tests to create http requests will fail.
|
|
|
|
|
|
|
|
example: setting an attribute on some class
|
|
|
|
------------------------------------------------------
|
|
|
|
|
2014-10-14 06:12:01 +08:00
|
|
|
If you need to patch out ``os.getcwd()`` to return an artificial
|
2013-09-27 18:33:06 +08:00
|
|
|
value::
|
|
|
|
|
|
|
|
def test_some_interaction(monkeypatch):
|
|
|
|
monkeypatch.setattr("os.getcwd", lambda: "/")
|
|
|
|
|
|
|
|
which is equivalent to the long form::
|
|
|
|
|
|
|
|
def test_some_interaction(monkeypatch):
|
|
|
|
import os
|
|
|
|
monkeypatch.setattr(os, "getcwd", lambda: "/")
|
|
|
|
|
2013-08-07 22:49:29 +08:00
|
|
|
|
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
Method reference of the monkeypatch function argument
|
|
|
|
-----------------------------------------------------
|
|
|
|
|
|
|
|
.. autoclass:: monkeypatch
|
2013-08-07 21:35:27 +08:00
|
|
|
:members: setattr, replace, delattr, setitem, delitem, setenv, delenv, syspath_prepend, chdir, undo
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
``monkeypatch.setattr/delattr/delitem/delenv()`` all
|
|
|
|
by default raise an Exception if the target does not exist.
|
|
|
|
Pass ``raising=False`` if you want to skip this check.
|
|
|
|
|