为了账号安全,请及时绑定邮箱和手机立即绑定

如何正确断言pytest中引发了异常?

如何正确断言pytest中引发了异常?

蝴蝶刀刀 2019-11-27 10:49:19
码:# coding=utf-8import pytestdef whatever():    return 9/0def test_whatever():    try:        whatever()    except ZeroDivisionError as exc:        pytest.fail(exc, pytrace=True)输出:================================ test session starts =================================platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2plugins: django, covcollected 1 items pytest_test.py F====================================== FAILURES ======================================___________________________________ test_whatever ____________________________________    def test_whatever():        try:            whatever()        except ZeroDivisionError as exc:>           pytest.fail(exc, pytrace=True)E           Failed: integer division or modulo by zeropytest_test.py:12: Failed============================== 1 failed in 1.16 seconds ==============================如何使pytest打印回溯,所以我会看到在whatever函数中引发异常的地方?
查看完整描述

3 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

您的意思是这样的吗:


def test_raises():

    with pytest.raises(Exception) as excinfo:   

        raise Exception('some info')

    # these asserts are identical; you can use either one   

    assert execinfo.value.args[0] == 'some info'

    assert str(execinfo.value) == 'some info'


查看完整回答
反对 回复 2019-11-27
  • 3 回答
  • 0 关注
  • 1666 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信