Testing in Python Fundamentals With Pytest

Testing is an essential part of developing applications. It helps ensure that the code works as expected and catches bugs early in the development process. In this article, we will explore the basics of testing in Python using the popular testing framework, Pytest.

What is Pytest?

Pytest is a testing framework for Python that makes writing and running tests simple and efficient. It provides a rich set of features for writing test cases, including fixtures, parameterized tests, and test discovery. Pytest is widely used in the Python community and is known for its simplicity and ease of use.

Setting Up Pytest

Installation

To install Pytest, use pip:

1
pip install pytest

To verify installation:

1
pytest --version

Basic Structure

Pytest automatically discovers tests based on a few conventions:

  1. Test files: Test files should start with test_ or end with _test.py.
  2. Test functions: Test functions should start with test_.
  3. Test classes: Test classes should start with Test.

Let’s create a simple project structure:

1
2
3
4
5
6
my_project/
│
├── src/
│   └── calculator.py
└── tests/
    └── test_calculator.py

Writing a simple test:

1
2
3
4
5
6
7
# src/calculator.py

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

def subtract(a, b):
    return a - b
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# tests/test_calculator.py

from src.calculator import add, subtract

def test_add():
    assert add(3, 5) == 8
    assert add(-1, 1) == 0
    assert add(-1, -1) == -2

def test_subtract():
    assert subtract(10, 5) == 5
    assert subtract(-1, 1) == -2
    assert subtract(-1, -1) == 0

To run the tests, simply navigate to your project directory and execute:

1
pytest

Pytest will automatically discover and run the test functions in the test_calculator.py file.

if you want to run a specific test file, you can specify the file name, even without following the naming convention, however, the function names should still start with test_:

1
pytest non_standard_test_file.py

Fixtures

Fixtures are functions that provide data to test functions. They help set up the test environment and provide reusable data for multiple tests. Fixtures are defined using the @pytest.fixture decorator.

Let’s create a fixture that provides a calculator object to our test functions:

1
2
3
4
5
6
7
8
import pytest 

@pytest.fixture
def some_data():
    return 42

def test_some_data(some_data):
    assert some_data == 42

In the above example, the some_data fixture provides the value 42 to the test_some_data test function. Fixtures can also be used to set up resources, such as database connections or temporary files.

Parameterized Tests

Parameterize tests to run with multiple sets of arguments.

1
2
3
4
5
6
7
8
9
import pytest

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

In the above example, the test_add function is parameterized with three sets of input arguments and expected results. Pytest will run the test with each set of arguments and report the results.

Python debugging with Pytest

Pytest provides a useful feature for debugging tests using the --pdb flag. When a test fails, you can drop into the Python debugger to inspect the state of the test environment.

1
pytest --pdb

This will drop you into the Python debugger when a test fails, allowing you to inspect variables, step through code, and diagnose the issue.

0%