fix path expansion example

This commit is contained in:
Evan Kepner 2019-05-27 23:23:18 -04:00
parent 24c95c78e7
commit 2dfbed11b4
No known key found for this signature in database
GPG Key ID: 25535B2446B36F2F
1 changed files with 13 additions and 15 deletions

View File

@ -63,40 +63,38 @@ testing, you do not want your test to depend on the running user. ``monkeypatch`
can be used to patch functions dependent on the user to always return a can be used to patch functions dependent on the user to always return a
specific value. specific value.
In this example, :py:meth:`monkeypatch.setattr` is used to patch ``os.path.expanduser`` In this example, :py:meth:`monkeypatch.setattr` is used to patch ``Path.home``
so that the known testing string ``"/abc"`` is always used when the test is run. so that the known testing path ``Path("/abc")`` is always used when the test is run.
This removes any dependency on the running user for testing purposes. This removes any dependency on the running user for testing purposes.
:py:meth:`monkeypatch.setattr` must be called before the function which will use :py:meth:`monkeypatch.setattr` must be called before the function which will use
the patched function is called. the patched function is called.
After the test function finishes the ``os.path.expanduser`` modification will be undone. After the test function finishes the ``Path.home`` modification will be undone.
.. code-block:: python .. code-block:: python
# contents of test_module.py with source code and the test # contents of test_module.py with source code and the test
# os.path is imported for reference in monkeypatch.setattr() from pathlib import Path
import os.path
def getssh(): def getssh():
"""Simple function to return expanded homedir ssh path.""" """Simple function to return expanded homedir ssh path."""
return os.path.expanduser("~/.ssh") return Path.home() / ".ssh"
def test_getssh(monkeypatch): def test_getssh(monkeypatch):
# mocked return function to replace os.path.expanduser # mocked return function to replace Path.home
# given a path, always return '/abc' # always return '/abc'
def mockreturn(path): def mockreturn():
return "/abc" return Path("/abc")
# Application of the monkeypatch to replace os.path.expanduser # Application of the monkeypatch to replace Path.home
# with the behavior of mockreturn defined above. # with the behavior of mockreturn defined above.
monkeypatch.setattr(os.path, "expanduser", mockreturn) monkeypatch.setattr(Path, "home", mockreturn)
# Calling getssh() will use mockreturn in place of os.path.expanduser # Calling getssh() will use mockreturn in place of Path.home
# for this test with the monkeypatch. # for this test with the monkeypatch.
x = getssh() x = getssh()
assert x == "/abc/.ssh" assert x == Path("/abc/.ssh")
Monkeypatching returned objects: building mock classes Monkeypatching returned objects: building mock classes
------------------------------------------------------ ------------------------------------------------------