日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 使用异常函数_您如何测试Python函数引发异常?

發(fā)布時(shí)間:2025/3/11 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 使用异常函数_您如何测试Python函数引发异常? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python 使用異常函數(shù)

This article elaborates on how to implement a test case for a function that raises an exception.

本文詳細(xì)介紹了如何為引發(fā)異常的函數(shù)實(shí)現(xiàn)測(cè)試用例

Consider the following function:

考慮以下功能:

import redef check_email_format(email):"""check that the entered email format is correct"""if not re.match(r"(^[a-zA-Z0-9_.+-][email?protected][a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email):raise Exception("Invalid email format")else:return "Email format is ok"

The above function validates the correctness of the email address. Also, note that the function raises an exception if the format is not correct.

以上功能可驗(yàn)證電子郵件地址的正確性。 另外,請(qǐng)注意,如果格式不正確,該函數(shù)將引發(fā)異常。

Now, to test such functionality, the pytest ( the testing module of python) has a built-in method called pytest.raises. The below test case helps in understanding the usage of pytest.raises method in order to assert is the method has raised an exception.

現(xiàn)在,為了測(cè)試這種功能, pytest (python的測(cè)試模塊)具有一個(gè)稱(chēng)為pytest.raises的內(nèi)置方法。 下面的測(cè)試用例有助于理解pytest.raises方法的用法,以便斷言該方法引發(fā)了異常。

In the below example, the test case is asserting if the "check_email_format" raises an exception if the email address is of the correct format, i.e., this is a negative test case, where we expect the test case to fail.

在下面的示例中,如果電子郵件地址的格式正確,則測(cè)試用例斷言“ check_email_format ”是否引發(fā)異常,即,這是一個(gè)否定的測(cè)試用例,我們期望測(cè)試用例失敗。

>>> import pytest >>> def test_email_exception(): ... """test that exception is raised for invalid emails""" ... with pytest.raises(Exception): ... assert check_email_format("[email?protected]")

Execution of test case:

執(zhí)行測(cè)試用例:

pytest invalid_test_case.py ========================================================================================== test session starts =========================================================================================== platform darwin -- Python 3.7.0, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 rootdir: /Users/XXX/XXX-work/src collected 1 item invalid_test_case.py F [100%]================================================================================================ FAILURES ================================================================================================ __________________________________________________________________________________________ test_email_exception __________________________________________________________________________________________def test_email_exception():"""test that exception is raised for invalid emails"""with pytest.raises(Exception): > assert check_email_format("[email?protected]") E Failed: DID NOT RAISE <class 'Exception'>invalid_test_case.py:15: Failed ========================================== 1 failed in 0.05s ================================================================

Now consider a positive testcase, where we expect the test case to pass i.e., we expect an exception to be raised.

現(xiàn)在考慮一個(gè)肯定的測(cè)試用例,我們希望測(cè)試用例能夠通過(guò),即,我們期望引發(fā)異常。

def test_email_exception():"""test that exception is raised for invalid emails"""with pytest.raises(Exception):assert check_email_format("bademail.com")

Execution of test case:

執(zhí)行測(cè)試用例:

pytest valid_test_case.py ======================================= test session starts =============================================================== platform darwin -- Python 3.7.0, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 rootdir: /Users/suri/preeti-work/python_ml_samples/src collected 1 item valid_test_case.py . [100%]========================================== 1 passed in 0.01s ================================================================

翻譯自: https://www.includehelp.com/python/how-do-you-test-that-a-python-function-throws-an-exception.aspx

python 使用異常函數(shù)

總結(jié)

以上是生活随笔為你收集整理的python 使用异常函数_您如何测试Python函数引发异常?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。