Python course / Applied
Testing
Write tests that fail when the code is wrong, and make hard-to-test code testable. A test that passes whatever the code does is worse than no test, because it buys false confidence.
- assertions
- boundaries
- unittest
- setUp
- subTest
- doctest
- mocks and patching
- async tests
Runs in your browser. Start straight away; create an account when you want your progress kept.
import unittest
def portion(total, people):
if people <= 0:
raise ValueError("people must be positive")
return total / people
class PortionTests(unittest.TestCase):
def test_shares(self):
for total, people, each in [(10, 4, 2.5), (9, 3, 3.0)]:
with self.subTest(total=total, people=people):
self.assertEqual(portion(total, people), each)
def test_refuses_nobody(self):
with self.assertRaises(ValueError):
portion(10, 0)
unittest.main(argv=["tests"], exit=False, verbosity=2)
test_refuses_nobody (__main__.PortionTests.test_refuses_nobody) ... ok test_shares (__main__.PortionTests.test_shares) ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK
What you will learn
- Write test functions that call code and assert what it should give, strong enough to fail when the code is wrong.
- Pick test cases at the boundaries and for the errors, where mistakes live, and test that code raises by hand.
- Write tests as a unittest.TestCase with assertEqual and assertRaises, and run them with unittest.main.
- Make code that reads the clock testable by passing the clock in, and test it with fakes.
- Replace something a function looks up for the length of a test with unittest.mock.patch, patching the name where it is used.
- Write tests that check what code does for its callers, so they survive a rewrite of how it does it.
The lessons in this module
- What a test is
- Choosing the cases
- unittest
- Fresh objects for every test: setUp
- Tables of cases: subTest
- Examples that check themselves: doctest
- Passing in what varies
- Mock objects
- Patching
- Testing input and output
- Testing with files
- Testing async code
- Testing properties
- Test behaviour, not insides
- Tests first
- A test for every bug
- Capstone: a test suite for an inventory
- Module review: a table-driven test runner
Ready when you are
Previous: Async syntax and event loops. Next: Patterns and architecture. All modules are on the course page.