第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

為什么 pytest 覆蓋跳過自定義異常消息斷言?

為什么 pytest 覆蓋跳過自定義異常消息斷言?

偶然的你 2022-06-02 17:55:14
我正在為 api 做一個(gè)包裝器。我希望該函數(shù)在輸入無效時(shí)返回自定義異常消息。def scrape(date, x, y):    response = requests.post(api_url, json={'date': date, 'x': x, 'y': y})    if response.status_code == 200:        output = loads(response.content.decode('utf-8'))        return output    else:        raise Exception('Invalid input')這是對它的測試:from scrape import scrapedef test_scrape():    with pytest.raises(Exception) as e:        assert scrape(date='test', x=0, y=0)        assert str(e.value) == 'Invalid input'但是覆蓋測試由于某種原因跳過了最后一行。有誰知道為什么?我嘗試將代碼更改為with pytest.raises(Exception,  match = 'Invalid input') as e,但出現(xiàn)錯(cuò)誤:AssertionError: Pattern 'Invalid input' not found in "date data 'test' does not match format '%Y-%m-%d %H:%M:%S'"這是否意味著它實(shí)際上是在引用來自 api 而不是我的包裝器的異常消息?
查看完整描述

2 回答

?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

由于引發(fā)的異常,它不會到達(dá)您的第二個(gè)斷言。你可以做的就是以這種方式斷言它的價(jià)值:


def test_scrape():

    with pytest.raises(Exception, match='Invalid input') as e:

        assert scrape(date='test', x=0, y=0)

我會說您在響應(yīng)時(shí)收到錯(cuò)誤“AssertionError: Pattern 'Invalid input' not found in "date data 'test' does not match format '%Y-%m-%d %H:%M:%S'"代碼是 200 - 所以沒有引發(fā)異常。


查看完整回答
反對 回復(fù) 2022-06-02
?
DIEA

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊

您的抓取函數(shù)引發(fā)異常,因此函數(shù)調(diào)用之后的行將不會執(zhí)行。您可以將最后一個(gè)斷言放在 pytest.raises 子句之外,如下所示:


from scrape import scrape


def test_scrape():

    with pytest.raises(Exception) as e:

        assert scrape(date='test', x=0, y=0)

    assert str(e.value) == 'Invalid input'


查看完整回答
反對 回復(fù) 2022-06-02
  • 2 回答
  • 0 關(guān)注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號