python 使用异常函数_您如何测试Python函数引发异常?
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)題。
- 上一篇: 字符串的回文子序列个数_计算给定字符串中
- 下一篇: 在C ++ STL中使用string :