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.pswp.cn/news/542571.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542571.shtml
英文地址,請注明出處:http://en.pswp.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方法…