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

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 使用异常函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542571.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 远程图片合拼,PHP实现将几张照片拼接到一起的合成图片功能【便于整体打印输出】...

本文实例讲述了PHP实现将几张照片拼接到一起的合成图片功能。共享给大家供大家参考&#xff0c;详细如下&#xff1a;/*** 作品合成程序* 针对单面&#xff0c;封面不做特殊处理*/$src_path $argv[1]; // php该文件&#xff0c;第一个参数是文件夹名(作品集)&#xff0c;可相对…

bandizip最后一个无广告版本_如果非要选择一款压缩软件的话——Bandizip

全世界只有不到0.00~1 % 的人关注了我们得到你的关注是小帮的幸运压缩解压软件是电脑一个必备软甲&#xff0c;前面的文章介绍了一款开源小巧无广告的压缩解压软件windows工具软件选择之压缩软件——7-Zip&#xff0c;如果有人用不惯的话可以试试今天的这款。Bandizip 是一款来…

[MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现

新人刚开始学习ASP.NET MVC&#xff0c;若有不足之处希望能得到您的指点&#xff0c;不胜感激&#xff01; 先来一张项目的层级结构图: Model&#xff1a;模型层&#xff0c;主要是各种类型、枚举以及ORM框架&#xff0c;框架完成数据库和实体类的映射。项目中选用了微软的开源…

日期getUTCSeconds()方法以及JavaScript中的示例

JavaScript日期getUTCSeconds()方法 (JavaScript Date getUTCSeconds() method) getUTCSeconds() method is a Dates class method and it is used to get seconds from the current time according to the UTC (Universal time coordinated). getUTCSeconds()方法是Date的类方…

dedecms 在模板里引入php文件夹,dedecms如何添加并引入php文件

前言&#xff1a;有些时候我们需要创建一些单独的PHP文件&#xff0c;但是随便放入的PHP文件是不能够编译织梦 dedecms的标签的&#xff0c;所以我们需要引入织梦标签的编译引擎方案。例如&#xff0c;我们在根目录创建 example.php&#xff0c;代码如下&#xff1a;<?php …

mybatisplus代码生成器_想做时间管理大师?你可以试试Mybatis Plus代码生成器

1. 前言对于写Crud的老司机来说时间非常宝贵&#xff0c;一些样板代码写不但费时费力&#xff0c;而且枯燥无味。经常有小伙伴问我&#xff0c;胖哥你怎么天天那么有时间去搞新东西&#xff0c;透露一下秘诀呗。好吧&#xff0c;今天就把Mybatis-plus的代码生成器分享出来&…

安装Oracle 11g RAC R2 之Linux DNS 配置

Oracle 11g RAC 集群中引入了SCAN(Single Client Access Name)的概念&#xff0c;也就是指集群的单客户端访问名称。SCAN 这个特性为客户端提供了单一的主机名&#xff0c;用于访问集群中运行的 Oracle 数据库。如果您在集群中添加或删除节点&#xff0c;使用 SCAN 的客户端无需…

c++ websocket客户端_websocket使用

websocket使用一、介绍在项目开发过程中&#xff0c;很多时候&#xff0c;我们不可避免的需要实现的一个功能&#xff1a; 服务端实时发送信息给客户端。比如实时公告、实时订单通知、实时报警推送等等&#xff0c;登录后的客户端需要知道与它相关的实时信息&#xff0c;以便进…

汉子编码比字母编码长_字母/博客作者编码问题(使用动态编程)

汉子编码比字母编码长Problem statement: 问题陈述&#xff1a; Shivang is a blog writer and he is working on two websites simultaneously. He has to write two types of blogs which are: Shivang是一位博客作家&#xff0c;他同时在两个网站上工作。 他必须写两种类型…

php parent报错,mac brew 安装php扩展报错:parent directory is world writable but not sticky

$ brew install php70-mcrypt报错&#xff1a;Error: parent directory is world writable but not sticky搜索到github的答案https://github.com/Homebrew/legacy-homebrew/issues/40345原因&#xff1a;/tmp目录权限不对$ ls -ld /private/tmp打印出来 /private/tmp 被标黄了…

在cordova中使用HTML5的多文件上传

2019独角兽企业重金招聘Python工程师标准>>> 我们先看看linkface给开放的接口&#xff1a; 字段类型必需描述api_idstring是API 账户api_secretstring是API 密钥selfie_filefile见下方注释需上传的图片文件 1&#xff0c;上传本地图片进行检测时选取此参数selfie_ur…

python dataframe切片_python pandas dataframe 行列选择,切片操作方法

SQL中的select是根据列的名称来选取&#xff1b;Pandas则更为灵活&#xff0c;不但可根据列名称选取&#xff0c;还可以根据列所在的position&#xff08;数字&#xff0c;在第几行第几列&#xff0c;注意pandas行列的position是从0开始&#xff09;选取。相关函数如下&#xf…

php根据设备判断访问,PHP判断设备访问来源

/*** 判断用户请求设备是否是移动设备* return bool*/function isMobile() {//如果有HTTP_X_WAP_PROFILE则一定是移动设备if (isset($_SERVER[HTTP_X_WAP_PROFILE])) {return true;}//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息if (isset($_SERVER[HTTP_VIA])…

机器学习 深度学习 ai_如何学习机器学习和人工智能?

机器学习 深度学习 aiSTRATEGY 战略 Learn theory practical aspects. 学习理论和实践方面的知识。 (At first get an overview of what you are going to learn). (首先获得要学习的内容的概述)。 Gain a good hold/insight on each concept. 掌握/理解每个概念。 If you …

linux常用命令和配置

2019独角兽企业重金招聘Python工程师标准>>> 启动php&#xff1a; /etc/init.d/php-fpm restart 查看PHP运行目录&#xff1a; which php /usr/bin/php 查看php-fpm进程数&#xff1a; ps aux | grep -c php-fpm 查看运行内存 /usr/bin/php -i|grep mem iptables如…

centos7时间同步_centos 8.x系统配置chrony时间同步服务

centos 8.x系统配置chrony时间同步服务CentOS 7.x默认使用的时间同步服务为ntp服务&#xff0c;但是CentOS 8开始在官方的仓库中移除了ntp软件&#xff0c;换成默认的chrony进行时间同步的服务&#xff0c;chrony既可以作为客户端向其他时间服务器发送时间同步请求&#xff0c;…

php可以用scanf,C/C++中 使用scanf和printf如何读入输出double型数据。

黄舟2017-04-17 13:47:232楼注意scanf函数和printf函数是不同寻常的函数&#xff0c;因为它们都没有将函数的参数限制为固定数量。scanf函数和printf函数又可变长度的参数列表。当调用带可变长度参数列表的函数时&#xff0c;编译器会安排float参数自动转换成为double类型&…

ICWAI和ICWA的完整形式是什么?

ICWAI / ICWA&#xff1a;印度成本与工程会计师协会/印度儿童福利法 (ICWAI / ICWA: Institute of Cost and Works Accountants of India / Indian Child Welfare Act) 1)ICWAI&#xff1a;印度成本与工程会计师协会 (1) ICWAI: Institute of Cost and Works Accountants of In…

深入研究java.lang.Runtime类【转】

转自&#xff1a;http://blog.csdn.net/lastsweetop/article/details/3961911 目录(?)[-] javalang 类 RuntimegetRuntimeexitaddShutdownHookremoveShutdownHookhaltrunFinalizersOnExitexecexecexecexecexecexecavailableProcessorsfreeMemorytotalMemorymaxMemorygcrunFina…

java队列实现限流,java中应对高并发的两种策略

目的&#xff1a;提高可用性通过ExecutorService实现队列泄洪//含有20个线程的线程池private ExecutorService executorService Executors.newFixedThreadPool(20);将有并发压力的下游代码放入到线程池的submit方法中&#xff0c;如下&#xff1a;//同步调用线程池的submit方法…