Files
the_information_nexus/tech_docs/python/pytest.md
2024-05-01 12:28:44 -06:00

3.3 KiB

pytest is an essential Python library for developers focused on testing their code. It provides a powerful yet simple framework for writing and running tests, supporting unit tests, functional tests, and more complex test scenarios. pytest stands out for its ease of use, detailed assertions, fixtures for setup and teardown, and support for parameterized testing, among other features. Here's a concise reference guide for common use cases with pytest:

pytest Reference Guide

Installation

pip install pytest

Basic Usage

Writing Your First Test

Tests are Python functions that start with test_. Place your test functions in files named test_*.py or *_test.py.

# content of test_sample.py
def func(x):
    return x + 1

def test_answer():
    assert func(3) == 4

Running Tests

Navigate to the directory containing your test file and run:

pytest

pytest will discover and run all tests following its naming conventions.

Assertions

pytest uses standard Python assert for verifying expectations and values in tests, providing detailed output on assertion failures.

def test_check_equal():
    assert func(4) == 5

Fixtures

Fixtures provide a powerful mechanism for setup and teardown operations, creating test data, and configuring the test environment.

import pytest

@pytest.fixture
def input_value():
    return 38

def test_divisible_by_2(input_value):
    assert input_value % 2 == 0

Parameterized Tests

Parameterize tests to run the same test function with different inputs.

@pytest.mark.parametrize("test_input,expected", [(1, 2), (2, 3), (3, 4)])
def test_eval(test_input, expected):
    assert func(test_input) == expected

Handling Exceptions

Test that a certain exception is raised using the pytest.raises context manager.

def test_exception():
    with pytest.raises(ValueError):
        raise ValueError

Skipping Tests

Skip a test function or expect it to fail.

@pytest.mark.skip(reason="not implemented yet")
def test_to_skip():
    pass

@pytest.mark.xfail
def test_might_fail():
    pass

Using Plugins and Integrations

pytest supports numerous plugins for integration with other tools and extending its functionality, such as pytest-django for Django projects, pytest-asyncio for testing asyncio code, and many more.

Configuration

Use a pytest.ini, tox.ini, or pyproject.toml file to configure pytest behavior, like custom markers, test paths, and plugin configurations.

Advanced Usage

pytest offers a broad spectrum of advanced features, including but not limited to test fixtures scopes, using mocks, and integrating with continuous integration (CI) tools.

Continuous Integration

pytest can easily integrate with CI tools like Jenkins, GitHub Actions, or GitLab CI, running your test suite on every push or pull request automatically.

pytest simplifies and accelerates the testing process, encouraging best practices in test writing and execution. Its extensive documentation and active community make it a go-to choice for Python developers looking to ensure their code meets the highest standards of quality.

pytest is favored in the Python community for its rich ecosystem, simplicity, and ability to handle not just simple unit tests but also complex functional testing for applications and libraries.