liveBook · Manning

Software update

As explained in Build Python Web Apps with Streamlit

Patching is a testing technique that temporarily replaces a referenced object, function, or dependency with a controlled substitute during execution.

Patching temporarily replaces a referenced dependency with a controlled substitute while a test runs. This prevents real external requests and makes responses predictable, observable, and repeatable. section 11.3.2

With unittest.mock, @patch('requests.get') supplies a mock object to the test. Configure its return value, call the backend, and verify both the result and the request arguments. section 11.3.2

A practical patched test typically:

  • Creates a fake response whose json method returns predefined data. section 11.3.2
  • Replaces the external request and invokes the method under test. section 11.3.2
  • Checks calls, returned collections, and selected values. section 11.3.2

Testing movie search

Patch requests.get, return two predefined movies, call search_movies("test query"), and assert the expected URL parameters, result count, identifiers, and titles. Additional tests cover no results and missing poster paths. section 11.3.2

The same isolation principle applies to Streamlit frontends, while AppTest additionally simulates clicks, text entry, and inspection of rendered output. section 11.4

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains decorators, configured mock responses, call assertions, and result checks without descending into mock-library internals.
Examples
Uses mocked movie-search responses, request-argument assertions, empty results, poster URLs, and Streamlit interaction simulation.
What it assumes
Assumes basic unit-testing setup and teaches dependency isolation through patching and mocking.
What you can do after
Apply it via unittest.mock to build deterministic tests that control dependencies and verify calls and outputs.
How it connects
Connects unit testing with mocking, external dependencies, HTTP requests, and user interaction.

Also explained in

Compare how 4 books explain software update →

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.