Testing with pytest

Find out your code works before someone else finds out it doesn't. You'll write test functions with assert and run them with pytest, read a failing test's report, test the edge cases and the errors, run one test with many examples, and use fixtures and temporary folders. Then you'll write the tests for an online shop's shipping costs and catch the bugs hidden in three copies of it.

Updated

Tests in Python

  1. Your First Tests with pytest (Journeyman): A test is a small function that runs your code on an example and makes sure the answer is right, and pytest finds every test, runs it, and shows exactly what came back when one fails.
  2. Testing Edge Cases and Errors (Journeyman): Bugs hide at the edges of what a function accepts, so good tests try zero and empty values, the smallest and biggest values allowed and each side of a boundary, make sure bad values are refused with pytest.raises, and run many examples through one test with parametrize.
  3. Fixtures and Temporary Files (Journeyman): A pytest fixture builds the setup several tests share and hands each test a fresh copy, tmp_path gives each test its own empty folder to write files in, and capsys lets a test read what a function printed.

Exercise: Shipping Cost Tests

  1. Exercise: Shipping Cost Tests (Journeyman): Write the pytest tests for Crafty Corner's working shipping rules, from its prices by weight and zone to free shipping, rush orders, errors and shipping labels, and prove they're good by catching three bugs the check plants in copies of the code.