O'Reilly logo
live online training icon Live Online training

Mastering Python’s pytest

Using pytest’s advanced features: Fixtures, plugins, and configuration options

Matt Harrison

Join Matt Harrison for a hands-on deep dive into pytest's advanced features. You'll explore pytest's built-in fixtures and plug-ins and learn how to define your own fixtures and write your own plug-ins. Using a sample project, you'll create fixtures and use parameterization against this project; you'll also explore pytest's rich configuration options and understand the optimal file layout. Along the way, you'll discover how to use pytest with tox and continuous integration solutions.

What you'll learn-and how you can apply it

By the end of this live online course, you’ll understand:

  • How to use pytest fixtures and parameterization
  • Configuration best practices

And you’ll be able to:

  • Write tests using pytest
  • Create fixtures for testing
  • Run code coverage on your code

This training course is for you because...

  • You're a Python programmer with experience using the pytest module, and you want to learn more about pytest and its advanced features.
  • You're a pytest user, and you want to create plugins or integrate with continuous integration platforms.

Prerequisites

Required materials and setup:

  • A machine with Python 3.6 (or above) installed and the ability to run IDLE—a Python editor that ships with Python (Run python3 -m idlelib.idle. Note that on Windows you need to open the Command Prompt and run python -m idlelib.idle.)

Recommended preparation:

About your instructor

  • Matt runs MetaSnake, a Python and Data Science training and consulting company. He has over 15 years of experience using Python across a breadth of domains: Data Science, BI, Storage, Testing and Automation, Open Source Stack Management, and Search.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Environment and installation (15 minutes)

  • Lecture and hands-on walkthrough: Creating a virtual environment to sandbox your code and install pytest into the sandbox

Parameterizing tests (20 minutes)

  • Lecture and hands-on exercise: Using the parametrize decorator to reduce boilerplate in testing
  • Q&A

Fixtures introduction (15 minutes)

  • Lecture and hands-on exercise: Module, class, and method fixtures
  • Break (10 minutes)

Pytest fixtures (20 minutes)

  • Lecture and hands-on exercise: Using built-in fixtures and defining your own with the fixture decorator
  • Q&A

Coverage (20 minutes)

  • Lecture and hands-on exercise: Using the code coverage plugin to measure the effectiveness of your tests
  • Q&A

Plugins (20 minutes)

  • Lecture and hands-on exercise: Explore plugin layout
  • Q&A

Break (10 minutes)

Configuration (20 minutes)

  • Lecture and hands-on exercise: Best practices for file layout and sharing fixtures and local plugins
  • Q&A

Tox and CI (20 minutes)

  • Lecture: Integrating your project with tox and continuous integration solutions like Jenkins or CircleCI

Wrap-up and Q&A (10 minutes)