Writing unit tests with unittest

20-minute lesson · module-testing-03

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Writing unit tests with unittest

20-minute lesson · module-testing-03

  • Describe how `unittest` organizes tests into TestCase classes and test methods.
  • Use assertion methods like `assertEqual` and `assertTrue`.
  • Run a test suite headlessly and check whether it succeeded.
  • Explain the advantages of a framework over hand-rolled assert bundles.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Unit Testing / Writing Unit Tests / Test First Development

Adaptation notes. Reading rewritten in our voice. The gpa_to_letter example is ours. The student writes a plain function; the hidden tests build a small unittest.TestCase, run it headlessly via TextTestRunner, and assert result.wasSuccessful() — proving the framework runs in the Pyodide sandbox.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.