This test uses a mock API client to isolate and test the fetch_user_name function without calling a real API.
PyTest
from unittest.mock import Mock
def fetch_user_name(api_client, user_id):
user = api_client.get_user(user_id)
return user['name']
# Create a mock API client
mock_api = Mock()
mock_api.get_user.return_value = {'id': 42, 'name': 'Alice'}
# Test fetch_user_name with the mock
result = fetch_user_name(mock_api, 42)
assert result == 'Alice'print('Test passed')
OutputSuccess
Important Notes
Mocking avoids slow or unreliable external calls during tests.
It helps test only the code you want, making tests simpler and faster.
Remember to set expected return values or side effects on mocks.
Summary
Mocking replaces real parts with fake ones to isolate code under test.
This makes tests faster, more reliable, and focused.
Use mocks to control dependencies and test different scenarios easily.
Practice
(1/5)
1. Why do we use mocking in pytest when testing a function that depends on an external API?
easy
A. To make the test code longer and more complex
B. To replace the external API with a fake version so tests run faster and are reliable
C. To avoid writing any assertions in the test
D. To connect to the real API every time the test runs
Solution
Step 1: Understand the purpose of mocking
Mocking replaces real parts like external APIs with fake versions to isolate the code under test.
Step 2: Identify benefits in testing context
This makes tests faster and more reliable because they don't depend on the real API's availability or speed.
Final Answer:
To replace the external API with a fake version so tests run faster and are reliable -> Option B
Quick Check:
Mocking isolates code = Replace real parts with fakes [OK]
Hint: Mocking swaps real parts with fakes for fast, reliable tests [OK]
Common Mistakes:
Thinking mocking makes tests more complex
Believing mocking skips assertions
Assuming mocking calls the real API
2. Which of the following is the correct way to mock a function get_data in pytest using the monkeypatch fixture?
easy
A. monkeypatch.setattr('module.get_data', lambda: 'mocked')
B. monkeypatch.mock('module.get_data', 'mocked')
C. monkeypatch.patch('module.get_data', lambda: 'mocked')
D. monkeypatch.replace('module.get_data', 'mocked')
Solution
Step 1: Recall monkeypatch syntax
The correct method to replace an attribute is setattr with the target path and replacement function.
Step 2: Verify the correct usage
monkeypatch.setattr('module.get_data', lambda: 'mocked') uses monkeypatch.setattr('module.get_data', lambda: 'mocked'), which correctly mocks the function.
Final Answer:
monkeypatch.setattr('module.get_data', lambda: 'mocked') -> Option A
Quick Check:
Use monkeypatch.setattr to mock functions [OK]
Hint: Use monkeypatch.setattr to replace functions in pytest [OK]
Common Mistakes:
Using non-existent monkeypatch methods like mock or patch
Passing string instead of a callable for function replacement
Confusing monkeypatch with unittest.mock syntax
3. Given the code below, what will be the output of the test?
A. The assertion expects the original value, but mocking changed it to 20
B. The monkeypatch syntax is incorrect
C. The function get_value is not defined
D. The test is missing a return statement
Solution
Step 1: Analyze the monkeypatch effect
The monkeypatch replaces get_value to return 20 instead of 10.
Step 2: Check the assertion
The assertion expects 10, but the mocked function returns 20, causing failure.
Final Answer:
The assertion expects the original value, but mocking changed it to 20 -> Option A
Quick Check:
Mock changes output, assertion expects old value [OK]
Hint: Assertion fails if expected value ignores mocking effect [OK]
Common Mistakes:
Thinking monkeypatch syntax is wrong
Assuming function is undefined
Believing test needs a return statement
5. You have a function send_email() that sends real emails. You want to test notify_user() which calls send_email()</>. How does mocking send_email() help isolate notify_user() in pytest?
hard
A. It disables notify_user completely
B. It makes notify_user send multiple emails automatically
C. It connects notify_user to the real email server faster
D. It prevents real emails from sending and lets you test notify_user's logic only
Solution
Step 1: Identify the problem with real send_email
Calling real send_email() during tests can send unwanted emails and slow tests.
Step 2: Understand mocking benefit
Mocking send_email() replaces it with a fake that does nothing or simulates success, isolating notify_user() logic.
Final Answer:
It prevents real emails from sending and lets you test notify_user's logic only -> Option D
Quick Check:
Mocking avoids side effects, isolates tested code [OK]
Hint: Mock side-effect functions to isolate main logic in tests [OK]
Common Mistakes:
Thinking mocking triggers real side effects
Assuming mocking disables the tested function
Believing mocking speeds up real connections
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.