All courses
· intermediate

Testing Python Code

Write tests that catch real mistakes and survive a refactor. What to assert, how to structure a suite with unittest, testing the failure paths, and replacing the awkward parts with test doubles.

Pathaxis · Original content

Written in house, from the work rather than from a syllabus. Every example is one we would use ourselves, and every exercise is checked against the same engine that grades your answer.

85 min 6 lessons 20 quiz questions 6 exercisesStart course

What you’ll learn

  • Write an assertion that reports what actually went wrong
  • Structure a test as arrange, act, assert
  • Choose what to test, and recognise a test that only repeats the code
  • Group tests in a TestCase and share setup without sharing state
  • Assert that the right exception is raised for the right reason
  • Replace a slow or awkward dependency with a fake you control

Before you start

  • · Comfortable writing functions and classes
  • · Familiar with exceptions and raising them

Syllabus

01

What a test is

Assertions, the shape of a test, and deciding what is worth testing.

02

Structure with unittest

Grouping tests, sharing setup, and running a suite.

03

Testing the hard parts

Failure paths, and dependencies you do not want to call for real.