liveBook · Manning

Integration test

As explained in Build Python Web Apps with Streamlit

An integration test evaluates whether multiple software components and their dependencies interact correctly as a combined system.

An integration test checks whether multiple software components work correctly together. A frontend and backend may each pass unit tests while failing when connected, so integration tests exercise their interaction and, when needed, their external dependencies. section 11.2.1 section 11.5

Build integration coverage after verifying the individual pieces:

  • Test the frontend and backend together while patching requests.get; this checks the application connection without relying on the live API. section 11.5.1
  • Test the backend and external API together without mocking the API; load the API key from secrets.toml and call the backend directly. section 11.5.1
  • Keep end-to-end tests distinct: they exercise the frontend, backend, and API together without mocks under real-world conditions. section 11.2.2
  • Run integration tests repeatedly as code changes, updating broken tests or adding coverage when new behavior appears. section 11.7

Movie search integration

A Streamlit test creates an AppTest instance, supplies a test API key, mocks requests.get, enters “test movie,” and clicks the search button. It then asserts that results appear in session state and that the expected number of expanders is rendered. A second test searches the live TMDB API for “ferris bueller,” asserting that results include Ferris Bueller’s Day Off from 1986.

Listing 11.9 Integration test
import pytest
from unittest.mock import patch, Mock
from mock_data import MOCK_MOVIES, MOCK_RESULTS
from streamlit.testing.v1 import AppTest

@pytest.fixture
def setup_app():
  at = AppTest.from_file("frontend.py")
  at.secrets["api_key"] = "test_api_key"
  return at

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

  at = setup_app
  at.run()

  # Enter search query and click button
  at.sidebar.text_input[0].set_value("test movie").run()
  at.sidebar.button[0].click().run()

  # Verify results are in session_state
  assert "results" in at.session_state
  assert at.session_state["results"] == MOCK_RESULTS

  # Verify expanders are displayed
  expected_expanders = len(MOCK_MOVIES)
  actual_expanders = len(at.expander)
  assert actual_expanders == expected_expanders, \
    f"Expected {expected_expanders} expanders, found {actual_expanders}"

  # Add more assertions as required

Use the complete listing to inspect a pytest integration test that combines AppTest-driven user interaction with a patched HTTP request. section 11.5.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Builds two integration paths with fixtures, simulated interaction, selective mocking, live API calls, and assertions.
Examples
Uses a Streamlit movie search, mocked HTTP results, session-state assertions, rendered expanders, and a live Ferris Bueller API search.
What it assumes
Assumes basic pytest, unit testing, mocking, assertions, fixtures, and Streamlit AppTest knowledge.
What you can do after
Diagnose integration failures by testing component boundaries separately from isolated units and complete end-to-end flows.
How it connects
Connects unit testing, end-to-end testing, mocking, frontend-backend interaction, external APIs, pytest, and Streamlit AppTest.

Also explained in

Compare how 31 books explain integration test →

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.