Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Python Advanced - Testing with Pytest

Writing and running tests using Pytest in Python

Pytest is a powerful testing framework for Python that makes it easy to write simple and scalable test cases. It supports fixtures, parameterized testing, and a wide range of plugins for extended functionality. This tutorial explores how to use Pytest for writing and running tests in Python.

Key Points:

  • Pytest is a powerful testing framework for Python.
  • Pytest supports fixtures, parameterized testing, and plugins.
  • Pytest is easy to use and integrates well with other Python libraries.

Installing Pytest

To use Pytest, you need to install it using pip:


pip install pytest
            

Writing Basic Test Cases

Pytest allows you to write test cases as functions. Here is an example:


# test_sample.py

def test_addition():
    assert 1 + 1 == 2

def test_subtraction():
    assert 2 - 1 == 1
            

In this example, two basic test cases are defined to test addition and subtraction.

Running Tests

You can run your tests using the pytest command in the terminal:


pytest
            

Pytest will automatically discover and run all the test cases in your project.

Using Fixtures

Fixtures in Pytest provide a way to set up and tear down resources for your tests. Here is an example:


# test_fixture.py

import pytest

@pytest.fixture
def sample_data():
    return {'key': 'value'}

def test_data(sample_data):
    assert sample_data['key'] == 'value'
            

In this example, a fixture is defined to provide sample data for the test case.

Parameterized Testing

Pytest supports parameterized testing, allowing you to run a test with multiple sets of data. Here is an example:


# test_parametrize.py

import pytest

@pytest.mark.parametrize("a,b,expected", [
    (1, 1, 2),
    (2, 3, 5),
    (4, 5, 9)
])
def test_addition(a, b, expected):
    assert a + b == expected
            

In this example, the test case is run with three different sets of data using the parametrize decorator.

Asserting Exceptions

Pytest provides a way to assert that exceptions are raised during tests. Here is an example:


# test_exceptions.py

import pytest

def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0
            

In this example, the test case asserts that a ZeroDivisionError is raised when dividing by zero.

Running Specific Tests

You can run specific tests or test files by specifying their names in the pytest command. Here is an example:


pytest test_sample.py
pytest test_sample.py::test_addition
            

In these examples, the first command runs all the tests in the test_sample.py file, and the second command runs the test_addition test case specifically.

Generating Test Reports

Pytest can generate detailed test reports in various formats using plugins. Here is an example of generating an HTML report:


pip install pytest-html

pytest --html=report.html
            

In this example, the pytest-html plugin is used to generate an HTML report of the test results.

Summary

In this tutorial, you learned about writing and running tests using Pytest in Python. Pytest provides powerful tools for writing test cases, using fixtures, parameterized testing, asserting exceptions, running specific tests, and generating test reports. Understanding Pytest is essential for ensuring the reliability and correctness of your Python code.