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




















































