python 使用異常函數
This article elaborates on how to implement a test case for a function that raises an exception.
本文詳細介紹了如何為引發異常的函數實現測試用例 。
Consider the following function:
考慮以下功能:
import re
def 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.
以上功能可驗證電子郵件地址的正確性。 另外,請注意,如果格式不正確,該函數將引發異常。
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.
現在,為了測試這種功能, pytest (python的測試模塊)具有一個稱為pytest.raises的內置方法。 下面的測試用例有助于理解pytest.raises方法的用法,以便斷言該方法引發了異常。
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.
在下面的示例中,如果電子郵件地址的格式正確,則測試用例斷言“ check_email_format ”是否引發異常,即,這是一個否定的測試用例,我們期望測試用例失敗。
>>> 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:
執行測試用例:
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.
現在考慮一個肯定的測試用例,我們希望測試用例能夠通過,即,我們期望引發異常。
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:
執行測試用例:
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 使用異常函數