DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

pytest Exceptions - Part I

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.

One of the advantages of pytest over the unittest module is that we don't need to use different assert methods on different data structures. So I'd rather use pytest.

A good test suite should test the expected behavior both when the input is correct and also when the input triggers some exception. Without testing the exception we cannot be sure that they will be really raised when necessary, and our tests covered all. So it is very important to test exceptions as well.

Let assume we have a basic function like below:

def divide(a, b):
    if b == 0:
        raise ValueError("Cannot divide by Zero")

    return a / b
Enter fullscreen mode Exit fullscreen mode

Related test should be like:

import pytest

from pytest_exception_handle import divide


def test_zero_division():
    assert divide(12, 3) == 4
Enter fullscreen mode Exit fullscreen mode

And with exception testing full test should be like:

import pytest

from pytest_exception_handle import divide


def test_zero_division():
    assert divide(12, 3) == 4


def test_zero_division_exception():
    with pytest.raises(ValueError):
        assert divide(1, 0)
Enter fullscreen mode Exit fullscreen mode

After you run pytest -v-v flag for verbose output, you should see an
output like below:

platform linux -- Python 3.6.8, pytest-3.8.1, py-1.8.0, pluggy-0.12.0 --
/tmp/python/pytest_exception_handle/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /tmp/python/pytest_exception_handle, inifile:

collected 2 items
tests/test_pytest_exception_handle/test_exception_basic.py::test_zero_division PASSED                                           [ 50% ]
tests/test_pytest_exception_handle/test_exception_basic.py::test_zero_division_exception PASSED                                 [100% ]
Enter fullscreen mode Exit fullscreen mode

If you want to test the error message too:

import pytest

from pytest_exception_handle import divide


def test_zero_division():
    assert divide(12, 3) == 4
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries. One of the advantages of pytest over the unittest module is that we dont need to use different assert methods on different data structures. So Id rather use pytest. A good test suite should test the expected behaviour both when the input is correct and also when the input triggers some exception.


def test_zero_division_exception():
    with pytest.raises(ValueError):
        assert divide(1, 0)


def test_zero_division_exception_with_message():
    with pytest.raises(ValueError, match="Cannot divide by Zero"):
        assert divide(1, 0)
Enter fullscreen mode Exit fullscreen mode

And yes we don't need a additional ValueError Exception for "zero division error" since python already has a built-in one:

import pytest


def test_zero_division_builtin():
    with pytest.raises(ZeroDivisionError):
        assert 1/0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)