Home [python] Mastering Unit Testing in Python with pytest
Post
Cancel

[python] Mastering Unit Testing in Python with pytest

Intro

Introduction

Unit testing is a crucial part of software development that ensures the individual components or units of code are working correctly. In Python, pytest is a popular testing framework that simplifies the process of writing and executing tests. This post will delve into the basics of Unit Testing in Python with pytest and provide examples to guide you through the testing process.

Getting Started with pytest

To begin unit testing with pytest, you first need to install the pytest library. You can do this using pip:

1
pip install pytest

Once pytest is installed, you can create your test files using the naming convention test_*.py or *_test.py. Within these files, you can define your test functions using the naming convention test_*.

Writing Test Functions

Test functions in pytest are written using Python’s assert statement. The assert statement checks if a given expression is True. Here’s an example test function in pytest:

1
2
3
def test_addition():
    result = 2 + 2
    assert result == 4

In the above example, the test function test_addition checks if the result of adding 2 + 2 is equal to 4. If the assertion fails, pytest will raise an assertion error.

Running Tests

You can run your pytest tests by executing the following command in your terminal:

1
pytest

pytest will automatically discover and run all test functions within your test files. It will output the results of each test, indicating if the test passed or failed.

Parameterized Tests

In pytest, you can create parameterized tests using the @pytest.mark.parametrize decorator. This allows you to run the same test with multiple input values. Here’s an example of a parameterized test:

1
2
3
4
5
6
7
8
9
10
import pytest

@pytest.mark.parametrize("input, expected", [
    (2, 4),
    (3, 6),
    (4, 8)
])
def test_multiplication(input, expected):
    result = input * 2
    assert result == expected

In the above example, the test_multiplication function is parameterized to test different input values and their expected results.

Fixtures

Fixtures in pytest are used to set up common data or test dependencies that can be used across multiple test functions. Fixtures are created using the @pytest.fixture decorator. Here’s an example of using fixtures:

1
2
3
4
5
6
7
8
9
import pytest

@pytest.fixture
def data():
    return [1, 2, 3, 4, 5]

def test_sum(data):
    result = sum(data)
    assert result == 15

In the above example, the data fixture provides a list of numbers that is used in the test_sum function to calculate the sum of the numbers.

Conclusion

Unit testing with pytest in Python is a powerful tool for ensuring the quality of your code. By writing test functions, running tests, utilizing parameterized tests, and fixtures, you can create comprehensive test suites to validate the functionality of your code. Start harnessing the power of pytest for unit testing in Python and improve the reliability of your software projects.

This post is licensed under CC BY 4.0 by the author.
Contents