liveBook · Manning

Test

As explained in Build Python Web Apps with Streamlit

A test function is a callable test definition that exercises software and compares observed results with expected results.

A search screen can look right until a user searches for something unexpected—or an update quietly changes what appears. A test function checks a specific behavior by running code and comparing what happened with what should have happened. Its assertions pass when those expectations hold and fail when they do not. section 11.8 section 11.0

def add(x, y):
    return x + y

def test_add():
    assert add(2, 3) == 5
    assert add(-1, 1) == 0

This small pytest test calls add with two inputs and uses assertions to check both results. Its test_ name lets pytest discover it. section 11.2.3

For an app, build a test around one behavior:

  • Set up what the test needs. A pytest fixture can supply the same backend instance to several tests. section 11.3.2
  • Control dependencies when testing a component alone. Mock an API response so a backend unit test checks your code rather than the network or API. section 11.3.1 section 11.3.2
  • Run the behavior and assert the expected result. Check useful details, not just whether the call completed. section 11.3.2

A movie search with no matches

A backend test supplies a mocked API response containing an empty results list, calls search_movies, and asserts that the returned value is an empty dictionary. Changing that expectation to an empty list makes the test fail; pytest displays the difference between [] and {}. The failure tells you to inspect the expectation as well as the application code. section 11.3.2 section 11.3.3

A frontend test can check visible behavior too. Streamlit’s AppTest runs the app, enters a search term, clicks the button, and lets the test inspect session state and displayed elements. One such test caught ratings showing as N/A: the UI read a nonexistent rating key instead of vote_average. Backend and frontend unit tests check components in isolation; integration and end-to-end tests check how they work together and across the full flow. section 11.4.1 section 11.4.2 section 11.2.2

Run tests again when code changes. A new failure may reveal broken behavior or an expectation that needs updating; new behavior may need a new test. Unit tests are useful to run frequently, while tests involving external services may be slower or costlier. section 11.7

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through setup, mocked dependencies, calls, assertions, failures, and UI interaction; does not claim the resulting suite is comprehensive.
Examples
Uses arithmetic assertions, mocked movie searches, simulated UI clicks, and a missing rating to demonstrate passing and failing tests.
What it assumes
Assumes Python functions and a Streamlit app; introduces pytest with a minimal test before using fixtures and mocks.
What you can do after
Apply it via pytest and Streamlit AppTest to check backend results and simulated frontend behavior.
How it connects
Connects assertions and pytest fixtures with mocking, unit tests, integration tests, end-to-end tests, and Streamlit AppTest.

Also explained in

Compare how 53 books explain 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.