pytest-mock is a plugin for pytest that simplifies mocking in tests by providing a mocker fixture. It helps replace parts of your system under test with mock objects easily and cleanly.
Click to reveal answer
beginner
How do you use the mocker fixture in a pytest test function?
You add mocker as a parameter to your test function. Then use mocker.patch() to replace a function or object with a mock during the test.
Click to reveal answer
intermediate
What is the difference between mocker.patch and unittest.mock.patch?
mocker.patch is a pytest-mock wrapper around unittest.mock.patch. It integrates better with pytest, automatically cleans up mocks after tests, and provides simpler syntax.
Click to reveal answer
intermediate
How does pytest-mock help with test cleanup?
pytest-mock automatically undoes all patches after each test, so you don't need to manually stop or reset mocks. This prevents side effects between tests.
Click to reveal answer
beginner
Give an example of mocking a function using pytest-mock.
Example:
```python
def test_example(mocker):
mock_func = mocker.patch('module.function_to_mock')
mock_func.return_value = 42
result = module.function_to_mock()
assert result == 42
```
Click to reveal answer
What is the main benefit of using pytest-mock over unittest.mock directly?
ARequires no imports
BAutomatic cleanup of mocks after each test
CSupports only Python 2
DFaster test execution
✗ Incorrect
pytest-mock automatically undoes patches after each test, preventing side effects and making tests cleaner.
How do you apply a mock patch using pytest-mock?
AUse mocker.mock('target')
BCall unittest.mock.patch directly without parameters
CUse mocker.patch('target') inside a test function with mocker parameter
DAdd @patch decorator to the test function
✗ Incorrect
pytest-mock provides the mocker fixture, and you use mocker.patch('target') inside your test function.
What does mocker.patch return?
AA mock object replacing the target
BThe original function
CA boolean indicating success
DNone
✗ Incorrect
mocker.patch returns a mock object that replaces the target during the test.
Which of these is true about pytest-mock's mocker fixture?
AIt is automatically available when pytest-mock is installed
BIt must be imported manually in each test file
CIt only works with unittest tests
DIt disables all mocks after the test
✗ Incorrect
When pytest-mock is installed, the mocker fixture is automatically available in pytest tests.
If you want to mock a method 'foo' in class 'Bar' in module 'baz', what is the correct patch target string?
A'baz.foo.Bar'
B'Bar.foo'
C'foo.Bar.baz'
D'baz.Bar.foo'
✗ Incorrect
The patch target string uses the full import path: 'baz.Bar.foo'.
Explain how pytest-mock simplifies mocking in pytest tests.
Think about how mocker.patch works and test cleanup.
You got /4 concepts.
Describe the steps to mock a function using pytest-mock and verify its return value.
Consider a simple example mocking a function to return a fixed value.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using pytest-mock in tests?
easy
A. To replace parts of code with mock objects during testing
B. To speed up test execution by skipping tests
C. To automatically generate test data
D. To format test output for better readability
Solution
Step 1: Understand pytest-mock functionality
pytest-mock is a plugin that helps replace parts of your code with mock objects during tests.
Step 2: Identify the main use case
This replacement allows you to control and verify behavior without running actual code parts.
Final Answer:
To replace parts of code with mock objects during testing -> Option A
Quick Check:
pytest-mock = mocking code parts [OK]
Hint: Remember pytest-mock is for replacing code parts with mocks [OK]
Common Mistakes:
Confusing mocking with test skipping
Thinking pytest-mock generates test data
Assuming pytest-mock formats output
2. Which of the following is the correct way to mock a function get_data in module my_module using pytest-mock?
easy
A. mocker.patch('my_module.get_data')
B. mocker.patch(get_data)
C. mocker.mock('my_module.get_data')
D. mocker.replace('my_module.get_data')
Solution
Step 1: Recall pytest-mock patch syntax
The correct syntax uses a string with the full path: mocker.patch('module.function').
Step 2: Identify correct option
mocker.patch('my_module.get_data') uses the correct string path format. Options A, B, and D use incorrect methods or arguments.
Final Answer:
mocker.patch('my_module.get_data') -> Option A
Quick Check:
Use string path in mocker.patch() [OK]
Hint: Use string 'module.function' in mocker.patch() [OK]
Common Mistakes:
Passing function object instead of string path
Using mocker.mock instead of patch
Using mocker.replace which doesn't exist
3. Given the code below, what will be the output of the test?
def fetch_data():
return 'real data'
def test_fetch(mocker):
mocker.patch('__main__.fetch_data', return_value='mocked data')
result = fetch_data()
assert result == 'mocked data'
print(result)
medium
A. real data
B. Test fails with AssertionError
C. mocked data
D. None
Solution
Step 1: Understand patch effect on fetch_data
The patch replaces fetch_data to always return 'mocked data'.
Step 2: Analyze test assertions and print
The assertion checks for 'mocked data' and passes. The print outputs 'mocked data'.
Final Answer:
mocked data -> Option C
Quick Check:
Patched function returns mocked data [OK]
Hint: Patched functions return mocked values, not real ones [OK]
Common Mistakes:
Expecting original function output
Confusing patch target string
Assuming patch does not affect print
4. Identify the error in the following test code using pytest-mock:
A. The return_value should be a string, not an integer
B. assert statement syntax is wrong
C. mocker.patch should be called after the function call
D. The patch target string 'api.call' is incorrect or incomplete
Solution
Step 1: Check patch target correctness
The patch target 'api.call' must be the full import path. If 'api' is not imported or incomplete, patch fails.
Step 2: Validate other parts
Return value as integer is valid, patch before call is correct, and assert syntax is fine.
Final Answer:
The patch target string 'api.call' is incorrect or incomplete -> Option D
Quick Check:
Patch target must be full import path [OK]
Hint: Always use full import path in patch target string [OK]
Common Mistakes:
Using partial or wrong patch target strings
Thinking return_value must be string
Calling patch after function call
5. You want to test a function process_data() that calls fetch_from_api(). Using pytest-mock, how do you mock fetch_from_api to return different values on consecutive calls: first 'data1', then 'data2'?
hard
A. mocker.patch('module.fetch_from_api', return_value=['data1', 'data2'])
B. mocker.patch('module.fetch_from_api', side_effect=['data1', 'data2'])
C. mocker.patch('module.fetch_from_api', side_effect='data1', 'data2')
D. mocker.patch('module.fetch_from_api', return_values=['data1', 'data2'])
Solution
Step 1: Understand side_effect usage
To return different values on consecutive calls, use side_effect with a list of return values.
Step 2: Identify correct syntax
mocker.patch('module.fetch_from_api', side_effect=['data1', 'data2']) correctly uses side_effect=['data1', 'data2']. Options B, C, and D use incorrect parameters or syntax.
Final Answer:
mocker.patch('module.fetch_from_api', side_effect=['data1', 'data2']) -> Option B
Quick Check:
Use side_effect list for multiple return values [OK]
Hint: Use side_effect list to mock multiple return values [OK]
Common Mistakes:
Using return_value with a list instead of side_effect
Incorrect side_effect syntax with multiple arguments
Using non-existent return_values parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.