Run a TestCase and read the result object
One test is wrong on purpose (a wallet that spent 10 cannot still have 100). Fix the expected value and run again.
unittest is the testing framework built into Python: tests are methods of a TestCase class with setUp and assertEqual-style checks. doctest runs the examples written inside docstrings, so documentation that shows >>> sessions stays true.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a TestCase for a function is_even(n) with three tests, and run it in the browser with a TextTestRunner writing to a hidden stream. Print result.testsRun and result.wasSuccessful().
Write a function initials(name) with a docstring containing two doctest examples, put it in a module file and run doctest.testmod.
Use subTest to check is_even for several numbers in one test method.
Explain it without notes
How do you write a unittest test, and what do setUp and tearDown do?
What is the difference between a failure and an error in unittest?
How does doctest work, and where does it fall short?
How would you move a legacy unittest suite to pytest?
What do the +ELLIPSIS and +NORMALIZE_WHITESPACE directives do?
Expected output
tests run: 4
failures: 1
errors: 0
FAILED: __main__.WalletTests.test_wrong_on_purpose
AssertionError: 90 != 100
was successful: False