Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Testing Your Code With pytest (Summary)

pytest offers a core set of productivity features to filter and optimize your tests along with a flexible plugin system that extends its value even further. Whether you have a huge legacy unittest suite or you’re starting a new project from scratch, pytest has something to offer you.

In this video course, you learned how to use:

  • Fixtures for handling test dependencies, state, and reusable functionality
  • Marks for categorizing tests and limiting access to external resources
  • Parametrization for reducing duplicated code between tests
  • Durations to identify your slowest tests
  • Plugins for integrating with other frameworks and testing tools

If you’d like to learn more about the concepts in this video course, check out:


Sample Code (.zip)

6.2 KB


Course Slides (.pdf)

1.0 MB

00:00 In the previous lesson, I showed you how to use the parametrize mark to feed data to your tests. In this lesson, I’ll summarize the course and point you at some pytest plugins.

00:11 pytest is a popular alternative to the standard library’s unittest framework. It allows you to write less boilerplate code than unittest and provides fixtures to manage your test data.

00:23 The file is a single place where you can store fixtures, monkey patches, or anything else that is common across your tests. And the oddly spelled parametrize allows you to feed different kinds of data to the same test, with a beautiful amount of info in the failure messages.

00:43 If you want to learn more, here’s the link to the well-written pytest documentation. pytest also supports plugins that either change its behavior or provide extra fixtures for you to use. Some popular ones are pytest-randomly.

00:59 This makes sure that the test suite is run in random order, which can help catch problems caused by stateful code. pytest-cov integrates the coverage library that measures how much of your code was run by the tests.

01:13 It’s a common best practice for coding teams to have a minimum coverage number, and the coverage tool measures this for you. pytest-django provides some fixtures that are useful if you’re testing Django code. And pytest-bdd gives you behavior driven development goodness by integrating with the Gherkin language from the Cucumber test mechanism.

01:36 Thanks for your attention. Oh, and that estimate you made—with apologies to Limp Bizkit, if I say test just 3 more times, that’ll be 184 tests in this testing rhyme.

01:48 I hope you enjoyed the course.

FooledByCode on June 22, 2022

Short and crisp, to the point, enough to get started. On thoughts of code coverage, where I work teams usually use SonarCube to get the estimated code coverage and many other things like security threats, security hot-spots, etc.

Christopher Trudeau RP Team on June 23, 2022

Yep, SonarQube is a great tool. I often have my teams use the coverage library as part of their local setup and then have SQ at the pull-request level.

FooledByCode on June 23, 2022

That’s exactly how we do it too.

Robert Curtiss on March 30, 2023

thank you, brief and very informative. I prefer this format, perhaps with a couple of brief tutorials building on the previous one. After completing each one, take a break to put in use what you have learned, and come back later to learn some advanced features. Cheers Bob

Become a Member to join the conversation.