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
|
2016-07-21 09:05:49 +08:00
|
|
|
tested such as network access. The ``monkeypatch`` fixture
|
2010-10-11 05:45:45 +08:00
|
|
|
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/
|
|
|
|
|
2016-07-21 09:05:49 +08:00
|
|
|
|
2012-01-07 04:37:18 +08:00
|
|
|
Simple example: monkeypatching functions
|
2019-05-12 21:52:22 +08:00
|
|
|
----------------------------------------
|
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
|
2018-05-18 16:19:46 +08:00
|
|
|
then calls into a function that calls it. After the test function
|
2012-01-07 04:37:18 +08:00
|
|
|
finishes the ``os.path.expanduser`` modification will be undone.
|
|
|
|
|
2019-05-12 21:52:22 +08:00
|
|
|
Global patch example: preventing "requests" from remote operations
|
|
|
|
------------------------------------------------------------------
|
2013-08-07 22:49:29 +08:00
|
|
|
|
|
|
|
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
|
2018-05-18 16:19:46 +08:00
|
|
|
will delete the method ``request.session.Session.request``
|
2013-09-27 18:33:06 +08:00
|
|
|
so that any attempts within tests to create http requests will fail.
|
|
|
|
|
2013-08-07 22:49:29 +08:00
|
|
|
|
2016-10-05 23:57:40 +08:00
|
|
|
.. note::
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2016-10-05 23:57:40 +08:00
|
|
|
Be advised that it is not recommended to patch builtin functions such as ``open``,
|
|
|
|
``compile``, etc., because it might break pytest's internals. If that's
|
2018-05-18 16:19:46 +08:00
|
|
|
unavoidable, passing ``--tb=native``, ``--assert=plain`` and ``--capture=no`` might
|
2017-01-01 01:54:47 +08:00
|
|
|
help although there's no guarantee.
|
2018-04-11 01:30:10 +08:00
|
|
|
|
2018-04-13 21:00:07 +08:00
|
|
|
.. note::
|
2018-04-11 01:30:10 +08:00
|
|
|
|
2018-04-13 21:00:07 +08:00
|
|
|
Mind that patching ``stdlib`` functions and some third-party libraries used by pytest
|
|
|
|
might break pytest itself, therefore in those cases it is recommended to use
|
|
|
|
:meth:`MonkeyPatch.context` to limit the patching to the block you want tested:
|
2018-04-11 01:30:10 +08:00
|
|
|
|
2018-04-13 21:00:07 +08:00
|
|
|
.. code-block:: python
|
2018-04-15 02:06:58 +08:00
|
|
|
|
2018-04-11 01:30:10 +08:00
|
|
|
import functools
|
2018-06-03 11:29:28 +08:00
|
|
|
|
|
|
|
|
2018-04-11 01:30:10 +08:00
|
|
|
def test_partial(monkeypatch):
|
|
|
|
with monkeypatch.context() as m:
|
|
|
|
m.setattr(functools, "partial", 3)
|
|
|
|
assert functools.partial == 3
|
2018-04-13 21:00:07 +08:00
|
|
|
|
|
|
|
See issue `#3290 <https://github.com/pytest-dev/pytest/issues/3290>`_ for details.
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2016-10-05 23:57:40 +08:00
|
|
|
|
2019-05-12 21:52:22 +08:00
|
|
|
Monkeypatching environment variables
|
|
|
|
------------------------------------
|
|
|
|
|
|
|
|
If you are working with environment variables you often need to safely change the values
|
|
|
|
or delete them from the system for testing purposes. ``Monkeypatch`` provides a mechanism
|
|
|
|
to do this using the ``setenv`` and ``delenv`` method. Our example code to test::
|
|
|
|
|
|
|
|
# contents of our original code file e.g. code.py
|
|
|
|
import os
|
|
|
|
|
|
|
|
def get_os_user_lower():
|
|
|
|
"""Simple retrieval function.
|
|
|
|
Returns lowercase USER or raises EnvironmentError."""
|
|
|
|
username = os.getenv("USER")
|
|
|
|
|
|
|
|
if username is None:
|
|
|
|
raise EnvironmentError("USER environment is not set.")
|
|
|
|
|
|
|
|
return username.lower()
|
|
|
|
|
|
|
|
There are two potential paths. First, the ``USER`` environment variable is set to a
|
|
|
|
value. Second, the ``USER`` environment variable does not exist. Using ``monkeypatch``
|
|
|
|
both paths can be safely tested without impacting the running environment::
|
|
|
|
|
|
|
|
# contents of our test file e.g. test_code.py
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_upper_to_lower(monkeypatch):
|
|
|
|
"""Set the USER env var to assert the behavior."""
|
|
|
|
monkeypatch.setenv("USER", "TestingUser")
|
|
|
|
assert get_os_user_lower() == "testinguser"
|
|
|
|
|
|
|
|
def test_raise_exception(monkeypatch):
|
|
|
|
"""Remove the USER env var and assert EnvironmentError is raised."""
|
|
|
|
monkeypatch.delenv("USER", raising=False)
|
|
|
|
|
|
|
|
with pytest.raises(EnvironmentError):
|
|
|
|
_ = get_os_user_lower()
|
|
|
|
|
|
|
|
This behavior can be be moved into ``fixture`` structures and shared across tests::
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_env_user(monkeypatch):
|
|
|
|
monkeypatch.setenv("USER", "TestingUser")
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_env_missing(monkeypatch):
|
|
|
|
monkeypatch.delenv("USER", raising=False)
|
|
|
|
|
|
|
|
# Notice the tests reference the fixtures for mocks
|
|
|
|
def test_upper_to_lower(mock_env_user):
|
|
|
|
assert get_os_user_lower() == "testinguser"
|
|
|
|
|
|
|
|
def test_raise_exception(mock_env_missing):
|
|
|
|
with pytest.raises(EnvironmentError):
|
|
|
|
_ = get_os_user_lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-28 04:48:19 +08:00
|
|
|
.. currentmodule:: _pytest.monkeypatch
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2018-02-28 04:48:19 +08:00
|
|
|
API Reference
|
|
|
|
-------------
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2018-02-28 04:48:19 +08:00
|
|
|
Consult the docs for the :class:`MonkeyPatch` class.
|