Python 5.6Code walkthrough8 parts

Testing with assert and pytest

Check that code does what it should, automatically and every time it changes.

In this lesson8 parts

  1. 01The bug that could come back
  2. 02assert
  3. 03A test file
  4. 04What to test
  5. 05Reading a failure
  6. 06Tests while you change code
  7. 07Where tests mislead
  8. 08Recap, your challenge, and next

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

assertion
A statement that a condition is True; assert raises AssertionError when it is not.
edge case
An input at the limit of what the code handles: an empty list, one item, an empty string.
error case
An input the code should refuse, checked by testing that it raises.
pytest
A tool that finds the test functions in a project and runs them all.
regression test
A test written from a bug, so that the bug can not come back unnoticed.
test
A function that runs code and asserts what it should give.

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.