Practice Bank: Testing Your Code
A deeper set of practice problems, organized by topic, for extra reps
beyond the main exercises — including scenario-based problems
written in the same style you'll meet in real interviews.
unittest and unittest.mock are standard
library, no installs needed; the pytest topic returns pytest-style
source as strings so the file still runs standalone.
starter.py below and run
python3 starter.py from inside the same folder.
unittest/unittest.mock are standard
library — nothing to install for those. If you want to
actually execute the pytest source strings from Topic 3, save them
to a file and pip install pytest first.
Topic 1: Why Testing Matters
1 Test by hand
Find # TODO 1.1. Write manual_check(fn, args, expected).
2 Would a test have caught it?
Find # TODO 1.2. Write would_have_caught_bug(fn, args, expected).
3 Debug the Code
Find # TODO 1.3. This swallows the real exception type and always reports "no error". Fix it.
Why We Test
Find # TODO 1.A. Write cost_of_bug_in_stage(stage).
Why manual testing doesn't scale
Find # TODO 1.B. Write explain_why_manual_testing_doesnt_scale().
Topic 2: Writing Tests with unittest
1 assertEqual basics
Find # TODO 2.1. Write AddTestCase using assertEqual.
2 assertRaises
Find # TODO 2.2. Write DivideTestCase using assertEqual and assertRaises.
3 Debug the Code
Find # TODO 2.3. This uses the wrong assert method for a boolean and calls unittest.main() unsafely inside a function. Fix it.
Rescuing an Untested Function
Find # TODO 2.A. Write unittest_covers_parse_price() covering a teammate's untested function.
Which assert method, when?
Find # TODO 2.B. Write explain_assert_methods().
Topic 3: Writing Tests with pytest
1 A plain-assert pytest test
Find # TODO 3.1. Write pytest_test_file_source().
2 pytest discovery rules
Find # TODO 3.2. Write pytest_naming_is_valid(name).
3 Debug the Code
Find # TODO 3.3. This matches the substring "assert" anywhere, including inside assertEqual calls. Fix it to only count genuine bare assert statements.
Migrating unittest to pytest
Find # TODO 3.A. Write convert_assert_equal_to_pytest(unittest_line).
pytest vs. unittest
Find # TODO 3.B. Write explain_pytest_vs_unittest().
Topic 4: Fixtures and setUp/tearDown
1 Fresh state per test
Find # TODO 4.1. Write CounterTestCase using setUp().
2 A pytest fixture
Find # TODO 4.2. Write pytest_fixture_source() using @pytest.fixture.
3 Debug the Code
Find # TODO 4.3. SharedStateTestCase leaks state between tests via a class attribute. Fix it with setUp().
Fixing a Flaky CI Suite
Find # TODO 4.A. Write tests_are_independent(results_in_order, results_reversed).
What setUp/tearDown are for
Find # TODO 4.B. Write explain_setup_teardown_purpose().
Topic 5: Mocking and Test Doubles
1 Patch a fake API response
Find # TODO 5.1. Write mocked_price_lookup(mock_price).
2 Mock two dependencies at once
Find # TODO 5.2. Write place_order_without_real_calls(ticker, quantity, fake_price).
3 Debug the Code
Find # TODO 5.3. This patches the wrong target twice instead of patching the database call. Fix it and assert on the right mock.
Fixing a Teammate's Wrong Mock
Find # TODO 5.A. Write identify_unused_mock(function_under_test_uses, mock_target).
What's worth mocking
Find # TODO 5.B. Write explain_when_to_mock().
Topic 6: Test Coverage & What Good Tests Look Like
1 Arrange-Act-Assert
Find # TODO 6.1. Write aaa_pattern_labels().
2 Edge cases for clamp()
Find # TODO 6.2. Write edge_cases_for_clamp().
3 Debug the Code
Find # TODO 6.3. This returns vague test names (test_1, test_2...). Fix it to return descriptive names.
Code Review Judgment Call
Find # TODO 6.A. Write should_keep_test(tests_public_behavior, is_redundant_with_existing).
Happy path vs. edge cases
Find # TODO 6.B. Write explain_happy_path_vs_edge_cases().
solution.py. Your exact
wording doesn't need to match on the explanation-style tasks
— the goal is that your program runs without errors and does
what each TODO asks. For the string-returning pytest tasks, try
pasting the result into a real .py file and running
it with pytest to confirm it actually passes.