liveBook · Manning

Mocking

As explained in Build Python Web Apps with Streamlit

Mocking is the process of replacing real dependencies with fake, controlled versions during testing.

Suppose a movie-search app calls an API. If that API is slow or unavailable, a unit test of the app’s own search logic should not fail for reasons outside the code being tested. Mocking replaces a real dependency with a fake, controlled version during a test. The test can then choose the dependency’s response without making a network request. section 11.3.1

Choose the mock at the boundary of the component under test:

  • For a backend test, replace `requests.get` and provide a fake API response. Check both the returned movie data and the arguments sent to the request function. section 11.3.2
  • For a frontend test, replace `Backend` with a class that has the methods the frontend expects but returns fixed movie results and a dummy poster URL. section 11.4.1

A controlled movie-search response

Decorate a backend test with `@patch('requests.get')`. Create a `Mock` response, set `mock_response.json.return_value` to `{'results': MOCK_MOVIES}`, and make the patched request return that response. Call `backend.search_movies('test query')`, then assert that the request used the expected URL and parameters and that the returned movies match the test data. Change the fake results to an empty list to test the no-results case without relying on the live API. section 11.3.2

Listing 11.5 Backend unit tests
import pytest
from unittest.mock import patch, Mock
from backend import Backend, SEARCH_URL
from mock_data import MOCK_MOVIES, MOCK_RESULTS

@pytest.fixture
def backend():
  api_key = "test_api_key"
  return Backend(api_key)

@patch('requests.get')
def test_search_movies_success(mock_get, backend):
  mock_response = Mock()
  mock_response.json.return_value = {'results': MOCK_MOVIES}
  mock_get.return_value = mock_response

  results = backend.search_movies("test query")

  mock_get.assert_called_once_with(
    SEARCH_URL,
    params={"api_key": backend.api_key, "query": "test query"}
  )
  assert len(results) == len(MOCK_MOVIES)
  for _id, movie in MOCK_RESULTS.items():
    assert _id in results
    assert results[_id]["title"] == movie["title"]

Shows the fixture, patched request, controlled JSON response, and assertions together in one backend test. section 11.3.2

Mocking keeps a unit test focused; it does not show that the real components work together. Use integration tests to check components together and end-to-end tests to check the full flow without mocking. section 11.3.1 section 11.8

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through patched requests, configured responses, assertions, and a replacement backend; leaves real component interaction to other test types.
Examples
Uses movie-search responses, an empty-results case, a fake backend, and Streamlit frontend interaction tests.
What it assumes
Assumes Python functions and app components; introduces the unit-test isolation model before showing how to configure mocks.
What you can do after
Apply it via `unittest.mock.patch` and `Mock` to test backend and frontend behavior without relying on live API responses.
How it connects
Connects unit testing with external dependencies, pytest fixtures, assertions, integration testing, and end-to-end testing.

Also explained in

Compare how 21 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.