Skip to content
← All Python tracks

Testing with pytest and Hypothesis

Python

Write tests that fail when the code breaks. Share setup with fixtures, swap slow dependencies for fakes, run one test over many cases, and let Hypothesis find the inputs you missed.

This track uses Python. You can make it your practice mode to browse related content across the site.

9 exercises

10 articles

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 Write Tests That Catch Real Bugs Read
  2. 2 Not solved yet. Catch the Broken Versions Practice · Easy
  3. 3 Share Setup and Cleanup with Fixtures Read
  4. 4 Not solved yet. Work Out Fixture Setup and Teardown Order Practice · Medium
  5. 5 Pass In Dependencies Instead of Patching Them Read
  6. 6 Not solved yet. Pass In a Fake Clock Practice · Easy
  7. 7 Not solved yet. Build a Rate Limiter That Reads a Fake Clock Practice · Medium
  8. 8 Run One Test over Many Cases with parametrize Read
  9. 9 Not solved yet. Expand parametrize Tables into Test Cases Practice · Medium
  10. 10 Let Hypothesis Find the Inputs You Missed Read
  11. 11 Not solved yet. Check a Run-Length Codec with Four Properties Practice · Hard
  12. 12 Test Sequences of Operations Against a Model Read
  13. 13 Not solved yet. Find the Shortest Sequence That Breaks an LRU Cache Practice · Hard
  14. 14 Not solved yet. Score a Test Suite with Mutation Testing Practice · Hard
  15. 15 Type-Check Your Tests and Fixtures Read
  16. 16 Not solved yet. Build a Typed Factory Fixture Practice · Hard
  17. 17 Lay Out a Project So pytest Imports the Right Files Read
  18. 18 Turn On pytest's Strict Checks Read
  19. 19 Allow assert in Tests but Not in App Code Read

Check yourself

6 questions · not saved while you're signed out

Use these questions to check your understanding and compare explanations. Your score does not affect track completion.

0 / 6 correct

This page_count rounds down when it should round up. Which test fails?

Choose one answer to reveal the explanations. Answers are kept only while this page is open.

def page_count(items: int, per_page: int) -> int:
    return items // per_page  # should round up: 25 items is 3 pages

def test_runs() -> None:
    page_count(25, 10)

def test_partial_page() -> None:
    assert page_count(25, 10) == 3

def test_returns_int() -> None:
    assert isinstance(page_count(25, 10), int)

def test_full_pages() -> None:
    assert page_count(20, 10) == 2
Question 1 of 6