Pytest Testing Idioms

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

Tested Code is Supposed to Throw Exception

import pytest

def test_something_that_should_throw_exception():
    with pytest.raises(Exception) as e_info:
        my_method()

If the tested code raises a more specific exception, you can use that instead:

def test_something_that_should_throw_exception():
    with pytest.raises(ValueError) as e_info:
        my_method()

  • How to I test the exception message?

Also see:

Python Language | Exceptions