liveBook · Manning

Chapter 11: Testing Streamlit apps

What you will learn in chapter 11 of Build Python Web Apps with Streamlit

This chapter introduces a testing strategy for Streamlit apps, covering automated tests at multiple levels to catch defects and prevent regressions as an app evolves.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces a testing strategy for Streamlit apps, covering automated tests at multiple levels to catch defects and prevent regressions as an app evolves.

Key terms: testing strategy, frontend, backend, external dependency, existing features, unit tests, integration tests, end-to-end tests, pytest, automated tests

11.1 MovieScout: An app to test

This section introduces MovieScout as a deliberately simple movie-search app and outlines its frontend-to-backend-to-API data flow as the subject for the chapter's tests.

Key terms: MovieScout, Streamlit frontend, backend, external movie search API, API response object, search results

11.1.1 Using the TMDB API to pull movie info

This section introduces TMDB as the third-party movie-data API for MovieScout and shows how to obtain a key and make a basic movie search request.

Key terms: TMDB, The Movie Database, TMDB API, third-party API, TMDB account, API registration, Personal, API Key, API Read Access Token, requests

11.1.2 The backend

This subsection implements a Backend class that searches TMDB movies and turns poster paths into complete image URLs.

Key terms: Backend class, search_movies, get_poster_url, TMDB API, search query, results list, dictionary, movie ID, API response, static method

11.1.3 The frontend

This section builds MovieScout’s simple Streamlit frontend, from securing its API key to searching for and displaying movie results.

Key terms: Search button, search_movies, st.session_state, results, backend.py, st.expander, get_poster_url, st.markdown, overview, rating

11.2 Testing the application

This section establishes the testing foundation for MovieScout by introducing test categories, the project’s testing strategy, and pytest.

Key terms: testing, reliable software development, unexpected inputs, components change over time, categories of tests, test approach, MovieScout, overall strategy, pytest, Python testing framework

11.2.1 Different kinds of tests

This section distinguishes manual from automated testing and defines the unit, integration, and end-to-end automated tests used in the chapter.

Key terms: manual testing, automated testing, unit tests, integration tests, end-to-end (E2E) tests, frontend, backend, code quality

11.2.2 Our testing approach

This section outlines MovieScout’s bottom-up testing strategy, progressing from isolated unit tests to a live end-to-end test.

Key terms: bottom-up testing approach, unit tests, integration tests, end-to-end test, Backend class, TMDB API, external dependency, mocked, AppTest, frontend

11.2.3 A quick introduction to pytest

This section introduces pytest by showing how to define, run, and interpret a minimal automated Python test.

Key terms: pytest, automated tests, test discovery, test_example.py, add, test_add, assert, pytest -v

11.3 Unit testing the backend

This section introduces unit testing the MovieScout backend, whose job is to query TMDB and format search results for the frontend.

Key terms: Backend class, TMDB API, search query, dictionary format, frontend, unit testing, individual components, isolation, backend

11.3.1 How unit testing works

This section explains how unit tests isolate a single component from external dependencies by using mocks and checking expected behavior through assertions.

Key terms: unit testing, Backend class, search_movies, external dependencies, requests module, TMDB API, mocking, mocks, API responses, setup phase

11.3.2 Writing unit tests for the backend

This section builds pytest unit tests for the MovieScout backend by using mock movie data, fixtures, patched HTTP calls, and assertions for normal and edge-case behavior.

Key terms: MOCK_MOVIE_1, MOCK_MOVIE_2, MOCK_MOVIES, MOCK_RESULTS, mock_data.py, pytest.fixture, fixture, Backend, dummy API key, @patch('requests.get')

11.3.3 Running the tests

This section shows how to run Backend unit tests with pytest, interpret a passing or failing result, and undo a deliberately introduced faulty expectation.

Key terms: pytest, test_backend.py, pytest -v, -v, verbose, test_search_movies_no_results, diff, expected return value, actual return value, []

11.4 Unit testing the Streamlit frontend

This section introduces Streamlit frontend unit testing as ordinary isolated unit testing plus simulated user interaction and output inspection through AppTest.

Key terms: unit testing, testing environment, tear down, external dependencies, patching, mocking, frontend, backend, user interaction, input values

11.4.1 Writing unit tests for the frontend

This section shows how to unit test MovieScout’s Streamlit frontend in isolation by mocking its backend, setting up an AppTest fixture, and asserting both initial UI state and simulated user interactions.

Key terms: Backend, MockBackend, MOCK_RESULTS, mock_data.py, unit testing, mock, pytest.fixture, setup_app, AppTest, AppTest.from_file

11.4.2 Running frontend tests

This section runs the frontend unit tests, uses their failure to uncover and fix a silent movie-rating display bug, and motivates integration testing next.

Key terms: pytest, pytest -v, frontend unit tests, rating_text, vote_average, rating, N/A, markdown elements, unit tests, frontend

11.5 Testing the integration between components

This section explains why integration tests are needed to verify that app components and external dependencies work correctly together, beyond isolated unit tests.

Key terms: integration test, unit test, components, external dependencies, TMDB API

11.5.1 Writing integration tests

This section writes two integration tests for MovieScout: one for the frontend and backend together with the API isolated, and one for the backend against the real TMDB API without the frontend.

Key terms: frontend, backend, external API, integration tests, setup_app fixture, Backend class, requests.get, mocking, patching, test_backend_api_integration

11.6 Testing the end-to-end flow

This section shows how an end-to-end test runs MovieScout as a whole against the real TMDB API and verifies that a real movie search produces displayed results.

Key terms: end-to-end tests, unit tests, integration tests, real-world conditions, external dependencies, test_e2e.py, AppTest, Backend, mock data, TMDB API

11.7 Evolving code and tests

This section explains how tests should evolve with application code, be run selectively and repeatedly, and be automated to catch problems early.

Key terms: automated tests, unit tests, integration tests, end-to-end tests, regression, application code, pytest, tag, filter, Git hooks

11.8 Summary

This summary recaps how to test Streamlit applications with pytest, mocks, AppTest, and automated test workflows.

Where this is used later

Concepts explained in this chapter

← Chapter 10: RAG and agentic apps with LangGraph and Streamlit Chapter 12: Packaging and deploying Streamlit apps →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.