O'Reilly logo
live online training icon Live Online training

PyTest Tricks and Tips

Robust code with the best testing techniques

Topic: Software Development
Noah Gift
Alfredo Deza

Continuous integration, continuous delivery, testing, and unit testing, in particular, are the principal pillars for writing robust software. Although Python comes with a unit testing library, it isn’t easily extensible, it’s hard to understand, and it doesn’t include an easy way to run and report on tests. Luckily, pytest, Coverage.py, molotov, GitHub Actions, pylint, and CircleCI offer techniques for robust testing that will help get your projects to a stable production state.

Join experts Alfredo Deza and Noah Gift to explore different types of testing, like functional and load testing, and learn how to use pytest to integrate testing and tooling into CI/CD platforms like Jenkins to ensure a smooth deployment (or release).

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

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

  • File and directory layouts for automated testing
  • pytest basic concepts and tooling
  • Fixtures, helpers, and utilities—and when to use them
  • pytest plug-ins and advanced features like parametrization
  • Python support for continuous integration

And you’ll be able to:

  • Enhance coverage and reporting and improve robustness with readable tests
  • Do load testing
  • Support different Python versions with a test matrix using GitHub Actions
  • Produce solid deployments with GitLab and GitHub automatically

This training course is for you because...

  • You’re a data scientist, student, or developer looking to improve your code quality and robustness.
  • You work with Python or a Python environment.
  • You want to become a better engineer.

Prerequisites

  • A working knowledge of Python

Recommended preparation:

Recommended follow-up:

About your instructors

  • Noah Gift is lecturer and consultant in both the UC Davis Graduate School of Management’s MSBA program and Northwestern’s graduate data science program, MSDS, where he teaches and designs graduate machine learning, AI, and data science courses and consults on machine learning and cloud architecture for students and faculty. These responsibilities include leading a multicloud certification initiative for students. He’s the author of close to 100 technical publications, including two books on subjects ranging from cloud machine learning to DevOps. Noah has approximately 20 years’ experience programming in Python. He’s a Python Software Foundation Fellow, an AWS Subject Matter Expert (SME) on machine learning, an AWS Certified Solutions Architect and AWS Academy Accredited Instructor, a Google Certified Professional Cloud Architect, and a Microsoft MTA on Python. Over his career, he’s served in roles ranging from CTO, general manager, and consulting CTO to cloud architect at companies including ABC, Caltech, Sony Imageworks, Disney Feature Animation, Weta Digital, AT&T, Turner Studios, and Linden Lab. In the last 10 years, he’s been responsible for shipping many new products that generated millions of dollars of revenue and had global scale. Currently, he’s consulting startups and other companies. Noah holds an MBA from UC Davis, an MS in computer information systems from Cal State Los Angeles, and a BS in nutritional science from Cal Poly San Luis Obispo.

  • Alfredo Deza is a passionate software engineer, speaker, author, and former Olympic athlete with almost two decades of DevOps and software engineering experience. He currently teaches several courses about Python and Machine Learning Engineering, and gives worldwide lectures about software development, personal development, and professional sports. Alfredo has written several books about DevOps and Python, and continues to share his knowledge about resilient infrastructure, testing, and robust development practices in courses, books, and presentations.

Schedule

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

Introduction to pytest (55 minutes)

  • Group discussion: What’s your experience with Python and testing?
  • Presentation: Creating a Python project for testing—file and directory layouts, test functions and test classes, and test helpers and utilities; getting started with pytest—installing pytest, simple pytest usage, basic pytest invocation, creating pytest reports, and configuring pytest Makefile integration
  • Hands-on exercise: Create your own Makefile
  • Q&A

Break (5 minutes)

Further pytest usage (55 minutes)

  • Group discussion: What’s your experience with PDB (Python Debugger) and pytest fixtures?
  • Presentation: Debugging pytest—configuring pytest breakpoints, integrating PDB with pytest, stopping pytest on failure, and filtering specific tests on the command line; using pytest fixtures—setting up a test with fixtures in pytest, sharing test data with pytest, parametrizing fixtures with pytest, and grouping tests by fixtures with pytest
  • Hands-on exercise: Create your own pytest fixture
  • Q&A

Break (5 minutes)

Advanced testing techniques (55 minutes)

  • Group discussion: What’s your experience with monkey patching and GitHub Actions?
  • Presentation: Monkey patching with pytest; temporary directories; exploring built-in fixtures with pytest; installing and using pytest plug-ins; using GitHub Actions; load testing Python code
  • Hands-on exercise: Create your own monkeypatch
  • Q&A

Break (5 minutes)

Case studies (45 minutes)

  • Group discussion: What’s your experience with continuous integration?
  • Presentation: Test system architecture; continuous integration and Python testing; unit testing; functional testing; load testing
  • Hands-on exercise: Create your own continuous integration implementation

Wrap-up and Q&A (15 minutes)