亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確斷言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 關注
  • 1778 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號