本文共 4195 字,大约阅读时间需要 13 分钟。
python 使用异常函数
This article elaborates on how to implement a test case for a function that raises an exception.
本文详细介绍了如何为引发异常的函数实现测试用例 。
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_.+-][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("")
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.1rootdir: /Users/XXX/XXX-work/srccollected 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("")E Failed: DID NOT RAISEinvalid_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.1rootdir: /Users/suri/preeti-work/python_ml_samples/srccollected 1 item valid_test_case.py . [100%]========================================== 1 passed in 0.01s ================================================================
翻译自:
python 使用异常函数
转载地址:http://twxzd.baihongyu.com/