Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To make tests robust and detailed we also should check all exceptions which can occur during code execution. To write assertions about raised exceptions, you can use pytestuse pytest.raises() as  as a context manager. For this example, we are going to write a test to check if the divide() function returns an exception during a dividing number by zero or not.  In this examplecase, all tests are written in the test_exceptions.py file. For the tutorial purpose, there is also created new file list_func.py which contains the extra function to test.  

Our project folder should look like this:

Code Block
languagetext
.
├── code_examples
│   ├── division_func.py
│   ├── __init__.py
│   └── list_func.py
└── tests
    ├── __init__.py
    ├── test_division_func.py
    └── test_exceptions.py

ZeroDivisionError example

For the record division() function looks like this:

...

To check is dividing by zero returns exception we are going to import pytest Pytest and write a test which that looks like this:

Code Block
languagepy
import pytest
from code_examples.division_func import division


def test_division_by_zero():
    with pytest.raises(ZeroDivisionError):
        division(1, 0)

...

This test uses a special context manager facility in pytestin Pytest, in which you run a block of code that you expect to raise an exception, and let pytest handle it. Your test will fail if the exception is not raised. 

Let's run a pytest Pytest and see the output. To run the only tests from the test_exceptions.py file pytest run should look like this:

...

If there is a need to have access to the actual exception info the pytest Pytest context manager optionally lets you add 'as your_text' like in the example below.  exception "exception_info" is an ExceptionInfo instance, which is a wrapper around the actual exception raised. The main attributes of interest are .type, .value and .traceback.

...

Moreover, there is also a change that arguments provided to the test_select_item_from_list function have an improper type. To verify that we are going to check if the TypeError exception was raised. Let's add another test to test_selected_item_from_list():

Code Block
languagepy
def test_select_item_from_list():
    with pytest.raises(IndexError):
        select_item_from_list([1, 2, 3], 10)

    with pytest.raises(TypeError):
        select_item_from_list('list A', 'index')

...

For ValueError example there is created new function called find_item_possition_in_list:

Code Block
languagepy
def find_item_position_in_list(lst, item):
    return lst.index(item)

...

As expected there is no change to find a position of element '1' in an empty list. Let's run the test and check the output. 

The ValueError raised and testand test_find_item_position_in_list test passed.




The official documentation with listed available python built-in exceptions: https://docs.python.org/3/library/exceptions.html

...