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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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:

Download

Sample Code (.zip)

6.2 KB

Download

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 conftest.py 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.

Become a Member to join the conversation.