🌱 Tim's Dev Wiki

Search IconIcon to open search

Pytest

Last updated September 30, 2022.

Pytest is a very popular and simple testing framework for running unit tests, integration tests and e2e tests in Python projects.

# Usage

The convention is to make a separate tests directory in your project root directory, then make test files with the name test_*.py. Inside these files, you just define functions and directly make assertions with the built-in Python assert. That’s it (for simple tests).

1
2
def test_foo():
    assert(do_foo() == ...)

Run the pytest CLI to execute tests:

1
2
3
4
5
6
7
pytest                           # Discover and run all tests.
pytest -s                        # Show output from the tests.
pytest -vv                       # Show verbose differences between expected and actual.

pytest test_file.py              # Run tests in only the specified file.
pytest test_file.py::TestClass   # Run tests in only the specified class.
pytest test_file.py::test_func   # Run only the selected test.

# Exceptions Tests

To test that a function correctly raises an exception, use with pytest.raises(ExceptionClassName):

1
2
3
4
5
6
import pytest

def test_foo_raises_bar():
    with pytest.raises(BarException):
        # If this doesn't raise a `BarException`, then the test is considered to have failed.
        do_foo()