PNL Learn

Testing Python

Prove your code works with unittest, pytest-style tests and TDD.

Every Python job asks for tests, and interviewers probe how you write them. You will write plain assert tests and unittest suites with setUp, tearDown and assertRaises, design tests around edge cases, boundaries and behavior, isolate code from clocks, networks and randomness with dependency injection, mocks, patch and fakes, turn bug reports into regression tests, read coverage for what it does and does not prove, and practice test-driven development. The exercises grade your tests the way a code reviewer would: they must pass against correct code and catch planted bugs.

IntermediatePython7 lessons76 exercisesAbout 6 h
Create a free account

What you will learn

  • assert-based tests
  • unittest
  • Edge cases and boundaries
  • subTest and parametrizing
  • Mocks, patch and fakes
  • Dependency injection
  • Regression tests and coverage
  • Test-driven development

Lessons

  1. 1

    Why Tests and assert-Based Tests

    Write small test functions that call your code, check the result with assert, and fail loudly when something breaks.

    11 exercises

  2. 2

    unittest

    Organize tests into TestCase classes, pick the right assert method, read a run's result, test for exceptions and build fresh fixtures with setUp and tearDown.

    13 exercises

  3. 3

    Designing Good Tests

    Choose the inputs that find bugs, keep one behavior per test, loop over cases with subTest, test behavior instead of implementation, and keep tests independent.

    11 exercises

  4. 4

    Test Doubles

    Isolate code from clocks, networks and randomness with dependency injection and Mock objects, and check every call a mock records.

    11 exercises

  5. 5

    Patching and Fakes

    Swap names with patch, patch.object and patch.dict, patch where a name is looked up, and choose between stubs, mocks, spies and fakes.

    11 exercises

  6. 6

    Test Strategy and Coverage

    Place tests on the pyramid, turn bug reports into regression tests, design tests from the spec and from the code, and read coverage for what it does and does not prove.

    10 exercises

  7. 7

    TDD, Putting It Together

    Work in red, green, refactor cycles, fake it and triangulate, build code that passes a suite someone else wrote, and write suites that check invariants and use a fake clock.

    9 exercises

How you practice

You practice in the browser and every exercise gives you feedback right away. This course uses these formats:

  • Code exercise: 27
  • Fix the bug: 15
  • Type the answer: 9
  • Multiple choice: 6
  • Fill in the blank: 5
  • Predict the output: 5
  • Match the pairs: 4
  • Select all that apply: 3
  • Spot the bug: 1
  • Reorder lines: 1

Aligned to

CS2023ACM / IEEE-CS / AAAISource
  • Software Development Fundamentals
    • SDF-PracticesSoftware Development Practices
  • Software Engineering
    • SE-ValidationSoftware Verification and Validation

Proto Node Labs is not affiliated with or endorsed by ACM / IEEE-CS / AAAI. Exam names are trademarks of their owners.