pytest
test automation
software testing
performance analysis
debugging

Printing test execution times and pinning down slow tests with py.test

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In the world of software development, testing plays a vital role in ensuring code quality and adherence to specifications. Automated testing frameworks like `pytest` streamline the process by providing tools to write and execute tests efficiently. However, as test suites grow, execution time can become a bottleneck. Identifying and optimizing slow tests can lead to a significant reduction in build time, which is crucial for maintaining rapid development cycles.

This article explores how to print test execution times and pinpoint slow tests using `pytest`. We'll delve into `pytest` configuration options and plugins that aid in the performance assessment of test suites.

Pytest Overview

`pytest` is a testing framework written in Python, primarily for writing simple unit tests and complex functional testing. It is simple to use, scales well, and integrates with various other tools and plugins. It provides:

  • A powerful yet simple tool for managing test cases.
  • Rich plugin architecture supporting various needs.
  • Integration with continuous integration systems.
  • Easy to adopt and learn with minimal test fixture dependency.

Measure Test Execution Time with Pytest

Measuring test execution time can help identify inefficient tests. You can configure `pytest` to report the time taken for each test run and, when necessary, sort them for analysis.

Basic Configuration

By default, `pytest` captures duration information but does not print it prominently. We need to instruct `pytest` to display detailed duration information. This can be achieved by enabling verbose output and using the `--durations=N` argument. Here’s a basic example:

  • `--durations=N`: This option shows the slowest `N` test durations at the end of the test run.
  • `-v` or `--verbose`: Instructs `pytest` to output more detailed information about which tests are running.
  • Refactor Large Tests: Break down large tests into smaller, more focused tests.
  • Use Mocks: Replace real system calls with mocks to speed up execution, particularly for tests involving database calls or HTTP requests.
  • Exclude Slow Tests from Regular Runs: Use markers to run slow tests selectively only when detailed testing is necessary. This can be set up with a custom marker and selective testing runs.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.