博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 使用异常函数_您如何测试Python函数引发异常?
阅读量:2529 次
发布时间:2019-05-11

本文共 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 RAISE 
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.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/

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-05熔断降级服务异常报警通知
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
Linux下Nginx安装
查看>>
LVM扩容之xfs文件系统
查看>>
Hbase记录-client访问zookeeper大量断开以及参数调优分析(转载)
查看>>
代码片段收集
查看>>