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 Pytest plugins and find out how to define your own fixtures and write your own plugins. Matt also walks you through pytest's rich configuration options and discusses 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:

  • By the end of this live online course, you’ll understand:
  • The 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

Materials or downloads needed in advance:

  • A machine with Python 3.6 installed and set up (You should be able to type “python3” on macOS or Linux systems or “python” on a Windows terminal and drop into the Python interpreter.)

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

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

Coverage (20 minutes)

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

Plugins (20 minutes)

  • Lecture and hands-on exercise: Explore plugin layout
  • Break (10 minutes)

Configuration (20 minutes)

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

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)