31 de janeiro de 2014

Sometimes only checking if an exception was raised is not sufficient

Sometimes only checking if an exception was raised is not sufficient. So, get information about the raised exception with py.test.

As py.test documentation mentions, excinfo is a py.code.ExceptionInfo instance. To get the real exception object, you must get excinfo.value

# my test case.
 
import pytest
 
def test_raises_must_be_numeric_exception():
    with pytest.raises(ValueError) as excinfo:
        my_function_checking_numeric_value('non-numeric data')

    assert excinfo.value.message == 'Must be numeric'

Eu sou Vinicius Assef, um programador do século passado que gosta de Python, pratica Lean Development e acredita em Deus. Você pode me contactar por email ou twitter.

Nenhum comentário:

Postar um comentário

Marcadores