pytest Exceptions - Part I
Tags: python, test, exceptions
Published: 2019-11-18 15:30:38

---
title: pytest Exceptions - Part I
published: true
tags: python, test, exceptions
canonical_url: https://tech.serhatteker.com/post/2019-08/pytest-exceptions-p1/
---
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:

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

    return a / b
```

Related test should be like:

```python
import pytest

from pytest_exception_handle import divide


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

And with exception testing full test should be like:

```python
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)
```

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

```sh
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% ]
```

If you want to test the `error message` too:

```python
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 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 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)
```

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


```python
import pytest


def test_zero_division_builtin():
    with pytest.raises(ZeroDivisionError):
        assert 1/0
```