David's Blog

A Comprehensive Guide to Pytest in Python

By David Li on 2024-06-07T12:11:18.000Z

A Comprehensive Guide to Pytest in Python

Pytest is a popular testing framework in Python that makes it easy to write simple and scalable test cases. In this article, we will explore the basics of Pytest, its features, and best practices for writing effective test cases.

Introduction to Pytest

Pytest is a powerful testing framework designed to help you write, organize, and execute tests in Python. It provides a rich set of features, including advanced assertion introspection, fixtures, parameterized tests, and plugins. Pytest is compatible with Python 2.7 and Python 3.5+ and can be easily integrated into any Python project.

Installing Pytest

To get started, you need to install Pytest using pip. Open a terminal and run the following command:

pip install pytest

Writing Your First Test

Pytest uses a simple naming convention to discover and run tests. Test functions should be prefixed with test_, and test classes should be named Test*. Let’s create a simple test case for a function that adds two numbers.

First, create a new file example.py with the following content:

def add(a, b):
    return a + b

Next, create a test file named test_example.py with the following content:

from example import add

def test_add():
    assert add(1, 2) == 3
    assert add(5, 7) == 12

To run the tests, simply execute pytest in the terminal:


You should see an output similar to this:

============================= test session starts ==============================
platform linux -- Python 3.8.5, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /path/to/your/project
collected 1 item

test_example.py .                                                        [100%]

============================== 1 passed in 0.03s ===============================

Pytest Features

Assert Statements

Pytest provides an advanced assertion introspection mechanism that produces more informative error messages. You can use the built-in assert statement in Python to compare the expected and actual results.

For example, if we change the test_add function to produce a failing test:

def test_add():
    assert add(1, 2) == 3
    assert add(5, 7) == 13

The output would be:

=========================== short test summary info ============================
FAILED test_example.py::test_add - assert 12 == 13
============================== 1 failed in 0.04s ===============================


Fixtures are reusable components that can be used to set up preconditions and clean up after tests. They help to keep your tests clean and modular. To create a fixture, use the @pytest.fixture decorator.

For example, let’s create a fixture for a sample database connection:

import pytest

def database_connection():
    connection = create_database_connection()
    yield connection

You can use this fixture in a test function by passing it as an argument:

def test_database_query(database_connection):
    result = database_connection.query("SELECT * FROM users")
    assert len(result) == 5

Parameterized Tests

Parameterized tests allow you to run the same test function with different inputs and expected outputs. This can help to reduce code duplication and make your tests more readable.

To create a parameterized test, use the @pytest.mark.parametrize decorator:

import pytest

@pytest.mark.parametrize("input1, input2, expected", [
    (1, 2, 3),
    (5, 7, 12),
    (0, 0, 0),
def test_add(input1, input2, expected):
    assert add(input1, input2) == expected


Pytest is a powerful and flexible testing framework that can help you write effective test cases for your Python projects. By utilizing its features like advanced assertion introspection, fixtures, and parameterized tests, you can create clean and maintainable test suites. Start using Pytest today to improve the quality and reliability of your Python code.

© Copyright 2023 by Astro Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2023-05-29.