Testing Python code with pytest, from basics to CI

Write tests that catch real bugs: fixtures, parametrisation, mocking, testing APIs and databases, coverage and running it all in CI

7 ratings 1,960 students Sample

Created by Lukas Brenner

  • Updated September 2026
  • English
  • Intermediate

What you will learn

  • Write clear pytest tests with plain assert statements
  • Share setup with fixtures and cover many cases with parametrize
  • Test exceptions, logs and command line output
  • Replace external services with mocks and fakes where it makes sense
  • Test database code and web endpoints with isolated test data
  • Measure coverage and run the suite automatically in CI

Course content

5 sections, 24 lessons, 4h 5m total

pytest essentials 5 lessons · 49 min
  • Video: Why we test and what pytest gives us Preview 8 min
  • Article: Your first tests and reading failures Preview 12 min
  • Article: Testing errors and edge cases 11 min
  • Article: Fixtures for clean setup 13 min
  • Quiz: Check your understanding 5 min
Parametrisation and property based testing 5 lessons · 52 min
  • Video: pytest.mark.parametrize 12 min
  • Article: Parametrizing fixtures 9 min
  • Video: Property based testing with Hypothesis 14 min
  • Video: Snapshot testing for larger outputs 9 min
  • Video: Markers, skips and expected failures 8 min
Mocks, fakes and external services 5 lessons · 52 min
  • Article: When to mock and when not to 9 min
  • Video: unittest.mock and monkeypatch 13 min
  • Video: Fakes for the payment provider 12 min
  • Video: Testing HTTP clients with respx 10 min
  • Video: Controlling time 8 min
Databases and web endpoints 4 lessons · 45 min
  • Video: Testing database code 14 min
  • Video: Testing a FastAPI application 12 min
  • Article: Async tests 9 min
  • Video: Contract tests for the API 10 min
Keeping the suite healthy 5 lessons · 47 min
  • Video: Coverage with pytest-cov 10 min
  • Video: Fast suites and parallel runs 10 min
  • Video: Test smells and refactoring tests 11 min
  • Video: Running tests in CI 11 min
  • Quiz: Final check 5 min

Requirements

  • Comfortable with Python functions, classes and modules
  • Python 3.12 or newer

Description

Most Python developers know they should write tests. Fewer enjoy it, usually because their tests are slow, fragile or test the wrong things. This course teaches pytest in a way that makes tests feel useful: short, fast and focused on behaviour you actually care about.

We work on a realistic codebase, a small invoicing service with business rules, a database and calls to an external payment API. You will add tests to code that has none, find real bugs on the way and refactor with confidence once the tests are in place.

What you will learn

  • How pytest discovers and runs tests, and how to read its output
  • Assertions, fixtures and parametrised tests
  • Testing exceptions, warnings, logs and output
  • Mocking external services without mocking everything
  • Testing code that uses a database and a web framework
  • Property based testing with Hypothesis
  • Coverage, test speed and running tests in CI

A practical philosophy

Good tests describe behaviour, not implementation. They fail when something users depend on breaks, and they keep passing when you rename an internal function. Throughout the course we look at test smells, such as excessive mocking or giant fixtures, and how to fix them.

You should be comfortable writing Python functions and classes. No testing experience is required, but if you have written some tests before, the later sections will still have plenty for you.

Who this course is for

  • Python developers who want to start testing properly
  • Developers whose existing tests are slow or fragile
  • Data and backend engineers preparing code for production

Instructor

Lukas Brenner

Backend engineer teaching API design, Python services and testing that catches real bugs

  • 4.4 instructor rating
  • 22 reviews
  • 7,060 students
  • 3 courses
  • Sample

I design and build backend services and APIs, mostly in Python with FastAPI and PostgreSQL. A large part of my job is reviewing API contracts, writing tests and figuring out why something that worked on a laptop fails in production.

My courses are opinionated but honest about trade offs. I show the approach I use, explain the alternatives and point out when the alternative is the better choice for your situation.

I like small, boring, well tested systems. If you finish one of my courses with a habit of writing a failing test before a fix, I consider that a success.

View profile

Student reviews

4.4 7 ratings
5 star 57%
4 star 29%
3 star 14%
2 star 0%
1 star 0%
Valentina C. Sep 28, 2026
Sample

Practical and honest

No dogma, just good reasons for each choice. Our CI is faster and our tests are clearer.

Hiroshi M. Sep 11, 2026
Sample

Fixtures finally clicked

The factory fixture pattern cleaned up hundreds of lines in our test suite.

Camila R. Aug 11, 2026
Sample

Found a real bug in my code

The boundary testing lesson made me check our own discount logic, and we had the same off by one error.

Erik L. Jul 12, 2026
Sample

Great on mocking judgement

I appreciated the clear rules on when not to mock. The respx lesson was new to me and very handy.

Adaeze O. Jun 4, 2026
Sample

Hypothesis section is excellent

I had heard of property based testing but never tried it. It found a rounding issue in my project on the first run.

Paul G. May 1, 2026
Sample

Solid database testing approach

Transaction rollback per test sped our suite up a lot. Would like more on Django specifically.

Ming H. Mar 15, 2026
Sample

Dense in places

Lots of content packed into short lessons. I needed to pause often, but the material is good.

$44.99 Enroll now
SampleSample catalog: the instructors, courses, reviews and stats on this site are demo content.