August 2018
Beginner
160 pages
4h 8m
English
The monkeypatch fixture works by replacing an attribute of an object by another object (often called a mock), restoring the original object at the end of the test. A common problem when using this fixture is patching the wrong object, which causes the original function/object to be called instead of the mock one.
To understand the problem, we need to understand how import and import from work in Python.
Consider a module called services.py:
import subprocessdef start_service(service_name): subprocess.run(f"docker run {service_name}")
In this code, we are importing the subprocess module and bringing the subprocess module object into the services.py namespace. That's why we call subprocess.run: we are accessing the
Read now
Unlock full access