0
0
PyTesttesting~20 mins

Why mocking isolates code under test in PyTest - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does mocking help isolate the code under test?

In software testing, mocking is used to replace parts of the system that the code under test interacts with. Why does this help isolate the code under test?

AMocking changes the code under test to use simpler algorithms.
BMocking replaces external dependencies so tests focus only on the code's logic without side effects.
CMocking automatically fixes bugs in the code under test.
DMocking speeds up the code execution by removing all function calls.
Attempts:
2 left
💡 Hint

Think about how external parts can affect test results and why isolating helps.

Predict Output
intermediate
2:00remaining
Output of test with mocked dependency

What will be the output of this pytest test when the external API call is mocked?

PyTest
import pytest
from unittest.mock import Mock

def fetch_data(api_client):
    return api_client.get_data()

def test_fetch_data():
    mock_api = Mock()
    mock_api.get_data.return_value = {'key': 'value'}
    result = fetch_data(mock_api)
    assert result == {'key': 'value'}
    print('Test passed')
ANo output
BAssertionError
CTypeError
DTest passed
Attempts:
2 left
💡 Hint

Consider what the mock returns and what the assertion checks.

assertion
advanced
2:00remaining
Correct assertion to verify mock call count

Which assertion correctly verifies that the mocked method send_email was called exactly twice during the test?

PyTest
from unittest.mock import Mock

mock_service = Mock()
mock_service.send_email()
mock_service.send_email()
Aassert mock_service.send_email.call_count == 2
Bassert mock_service.send_email.called == 2
Cassert mock_service.send_email.call_args == 2
Dassert mock_service.send_email.call_count() == 2
Attempts:
2 left
💡 Hint

Check the correct attribute that counts calls on a mock method.

🔧 Debug
advanced
2:00remaining
Identify the error in this mocking code

What error will this pytest test raise when run?

PyTest
import pytest
from unittest.mock import patch

def get_user_name(user_id):
    # Imagine this calls a real database
    pass

def test_get_user_name():
    with patch('__main__.get_user_name') as mock_get:
        mock_get.return_value = 'Alice'
        result = get_user_name(1)
        assert result == 'Alice'
AModuleNotFoundError
BNo error, test passes
CAttributeError
DTypeError
Attempts:
2 left
💡 Hint

Check the patch target string and what it should include.

framework
expert
3:00remaining
Best practice for mocking in pytest fixtures

Which pytest fixture setup correctly mocks a database call for all tests in a module, ensuring isolation and cleanup?

A
@pytest.fixture(scope='module', autouse=True)
def mock_db_call(monkeypatch):
    monkeypatch.setattr('module.db_call', lambda: 'mocked')
    yield
    # no cleanup needed
B
@pytest.fixture(scope='module')
def mock_db_call(monkeypatch):
    monkeypatch.setattr('module.db_call', lambda: 'mocked')
    yield
    monkeypatch.undo()
C
@pytest.fixture(autouse=True)
def mock_db_call(monkeypatch):
    monkeypatch.setattr('module.db_call', lambda: 'mocked')
    yield
    # no cleanup needed
D
@pytest.fixture(scope='session')
def mock_db_call(monkeypatch):
    monkeypatch.setattr('module.db_call', lambda: 'mocked')
    yield
    monkeypatch.undo()
Attempts:
2 left
💡 Hint

Consider scope and autouse to apply mocks for all tests in a module and proper cleanup.