Skip to content

Latest commit

 

History

History
30 lines (22 loc) · 477 Bytes

PT017.md

File metadata and controls

30 lines (22 loc) · 477 Bytes

PT017

found assertion on exception {name} in except block, use pytest.raises() instead

Examples

Bad code:

def test_foo():
    try:
        1 / 0
    except ZeroDivisionError as e:
        assert e.args

Good code:

import pytest

def test_foo():
    with pytest.raises(ZeroDivisionError) as e:
        1 / 0
    assert e.value.args

Rationale

  • to avoid the situations when the test incorrectly passes because exception was not raised