python:pytest中的setup和teardown

原文:https://www.cnblogs.com/peiminer/p/9376352.html 

之前我寫的unittest的setup和teardown,還有setupClass和teardownClass(需要配合@classmethod裝飾器一起使用),接下來就介紹pytest的類似于這類的固件。

(1.setup_function、teardown_function 2.setup_class、teardown_class 3.setup_method、teardown_method 4.setup_module、teardown_module)

setup/teardown和unittest里面的setup/teardown是一樣的功能,這里setup_method和teardown_method的功能和setup/teardown功能是一樣的,優先級是先執行setup_method,在執行setup。一般二者用其中一個即可就不詳細介紹了。setup_class和teardown_class等價于unittest里面的setupClass和teardownClass

一、函數級的(setup_function、teardown_function)只對函數用例生效,而且不在類中使用

#!/usr/bin/env/python
# -*-coding:utf-8-*-import pytest"""
只對函數用例生效,不在類中
setup_function
teardown_function
"""def setup_function():print "setup_function():每個方法之前執行"def teardown_function():print "teardown_function():每個方法之后執行"def test_01():print "正在執行test1"x = "this"assert 'h' in xdef test_02():print "正在執行test2"x = "hello"assert hasattr(x,"hello")def add(a,b):return a+bdef test_add():print "正在執行test_add()"assert add(3,4) == 7if __name__=="__main__":pytest.main(["-s","test_function.py"])

?



運行結果為:(-s為了顯示用例的打印信息 -q只顯示結果不顯示過程)
可以看出執行的結果是:
setup_function--》 test_01 --》teardown_function
setup_function--》 test_02 --》teardown_function
setup_function--》 test_add --》teardown_function

二、類級的(setup_class、teardown_class)在類中使用,類執行之前運行一次,類執行之后運行一次

?

#!/usr/bin/env/python
# -*-coding:utf-8-*-"""
在類之前和之后執行一次
setup_class
teardown_class
"""import pytestclass TestClass(object):def setup_class(self):print "setup_class(self):每個類之前執行一次"def teardown_class(self):print "teardown_class(self):每個類之后執行一次"def add(self,a,b):print "這是加法運算"return a+bdef test_01(self):print "正在執行test1"x = "this"assert 'h' in xdef test_add(self):print "正在執行test_add()"assert self.add(3, 4) == 7

?


執行結果:
可以看出執行的順序是 setup_class --》 test1 --》test_add()--》teardown_class

三、類中方法級的(setup_method、teardown_method)在每一個方法之前執行一次,在每一個方法之后執行一次

#!/usr/bin/env/python
# -*-coding:utf-8-*-"""
開始于方法始末(在類中)
setup_method
teardown_method
"""
import pytestclass TestMethod(object):def setup_class(self):print "setup_class(self):每個類之前執行一次\n"def teardown_class(self):print "teardown_class(self):每個類之后執行一次"def setup_method(self):print "setup_method(self):在每個方法之前執行"def teardown_method(self):print "teardown_method(self):在每個方法之后執行\n"def add(self,a,b):print "這是加法運算"return a+bdef test_01(self):print "正在執行test1"x = "this"assert 'h' in xdef test_add(self):print "正在執行test_add()"assert self.add(3, 4) == 7

?


執行結果: setup_class --》 setup_method -->test1 -->teardown_method --》setup_method --> test_add()--》teardown_method --> teardown_class

四、模塊級的(setup_module、teardown_module)全局的,在模塊執行前運行一遍,在模塊執行后運行一遍

#!/usr/bin/env/python
# -*-coding:utf-8-*-import pytest
"""
開始于模塊始末,全局的
setup_module
teardown_module
"""def setup_module():print "setup_module():在模塊最之前執行\n"def teardown_module():print "teardown_module:在模塊之后執行"def setup_function():print "setup_function():每個方法之前執行"def teardown_function():print "teardown_function():每個方法之后執行\n"def test_01():print "正在執行test1"x = "this"assert 'h' in xdef add(a,b):return a+bdef test_add():print "正在執行test_add()"assert add(3,4) == 7

?


運行結果:setup_module --> setup_function --> test_01--> teardown_function --> setup_function --> test_add()--> teardown_function --> teardown_module

五、當類和函數都有的時候

#!/usr/bin/env/python
# -*-coding:utf-8-*-"""
在類之前和之后執行一次
setup_class
teardown_class
"""import pytestdef setup_module():print "setup_module():在模塊最之前執行\n"def teardown_module():print "teardown_module:在模塊之后執行"def setup_function():print "setup_function():每個方法之前執行"def teardown_function():print "teardown_function():每個方法之后執行\n"def test_10():print "正在執行test1"x = "this"assert 'h' in xdef add0(a,b):return a+bdef test_add():print "正在執行test_add()"assert add0(3,4) == 7class TestClass(object):def setup_class(self):print "setup_class(self):每個類之前執行一次"def teardown_class(self):print "teardown_class(self):每個類之后執行一次"def add(self,a,b):print "這是加法運算"return a+bdef test_01(self):print "正在執行test1"x = "this"assert 'h' in xdef test_add(self):print "正在執行test_add()"assert self.add(3, 4) == 7if __name__=="__main__":pytest.main(["-s","test_class0.py"])

?


運行結果:可以看出來,都互不影響,setup_module還是在最之前執行,所有之后執行。
setup_modele --> setup_function -->test1 -->teardown_function --> setuo_function -->test_add -->teardown_function -->setup_class -->teardown_class-->taerdown_module

轉載于:https://www.cnblogs.com/gcgc/p/11513184.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391278.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391278.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391278.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何開始使用任何類型的數據? - 第1部分

從數據開始 (START WITH DATA) My data science journey began with a student job in the Advanced Analytics department of one of the biggest automotive manufacturers in Germany. I was nave and still doing my masters.我的數據科學之旅從在德國最大的汽車制造商之一…

iHealth基于Docker的DevOps CI/CD實踐

本文由1月31日晚iHealth運維技術負責人郭拓在Rancher官方技術交流群內所做分享的內容整理而成,分享了iHealth從最初的服務器端直接部署,到現在實現全自動CI/CD的實踐經驗。作者簡介郭拓,北京愛和健康科技有限公司(iHealth)。負責公…

從早期的初創企業到MongoDB的經理(播客)

In this weeks podcast episode, I chat with Harry Wolff, an engineering manager at MongoDB in New York City. Harry has been in the world of tech for over a decade, holding jobs in various startups before ending up at Mongo. 在本周的播客節目中,我與…

leetcode 1011. 在 D 天內送達包裹的能力(二分法)

傳送帶上的包裹必須在 D 天內從一個港口運送到另一個港口。 傳送帶上的第 i 個包裹的重量為 weights[i]。每一天,我們都會按給出重量的順序往傳送帶上裝載包裹。我們裝載的重量不會超過船的最大運載重量。 返回能在 D 天內將傳送帶上的所有包裹送達的船的最低運載…

python:pytest優秀博客

上海悠悠:https://www.cnblogs.com/yoyoketang/tag/pytest/ 轉載于:https://www.cnblogs.com/gcgc/p/11514345.html

uva 11210

https://uva.onlinejudge.org/index.php?optioncom_onlinejudge&Itemid8&pageshow_problem&problem2151 題意:給你十三張麻將,問你需要哪幾張牌就可以胡牌,這個胡牌排除了七小對以及十三幺 胡牌必須要有一個對子加n個…

機器學習圖像源代碼_使用帶有代碼的機器學習進行快速房地產圖像分類

機器學習圖像源代碼RoomNet is a very lightweight (700 KB) and fast Convolutional Neural Net to classify pictures of different rooms of a house/apartment with 88.9 % validation accuracy over 1839 images. I have written this in python and TensorFlow.RoomNet是…

leetcode 938. 二叉搜索樹的范圍和

給定二叉搜索樹的根結點 root,返回值位于范圍 [low, high] 之間的所有結點的值的和。 示例 1: 輸入:root [10,5,15,3,7,null,18], low 7, high 15 輸出:32 示例 2: 輸入:root [10,5,15,3,7,13,18,1,nul…

456

456 轉載于:https://www.cnblogs.com/Forever77/p/11517711.html

課后作業-結隊編程項目進度-貪吃蛇

當前進度: 1.完成了窗口和蛇的繪制 2控制蛇的放向 3.繪制食物,隨機出現 4.設計暫停鍵和開始鍵 有遇到過問題,但通過上網和向同學請教解決了轉載于:https://www.cnblogs.com/qwsa/p/7605384.html

一百種簡單整人方法_一種非常簡單的用戶故事方法

一百種簡單整人方法User stories are a great way to plan development work. In theory. But how do you avoid getting burned in practice? I propose a radically simple approach.用戶故事是計劃開發工作的好方法。 理論上。 但是,如何避免在實踐中被燙傷&…

COVID-19和世界幸福報告數據告訴我們什么?

For many people, the idea of ??staying home actually sounded good at first. This process was really efficient for Netflix and Amazon. But then sad truths awaited us. What was boring was the number of dead and intubated patients one after the other. We al…

Python:self理解

Python類 class Student:# 類變量,可以通過類.類變量(Student.classroom)或者實例.類變量(a.classroom)方式調用classroom 火箭班def __init__(self, name, age):# self代表類的實例,self.name name表示當實例化Student時傳入的name參數賦值給類的實例…

leetcode 633. 平方數之和(雙指針)

給定一個非負整數 c ,你要判斷是否存在兩個整數 a 和 b,使得 a2 b2 c 。 示例 1: 輸入:c 5 輸出:true 解釋:1 * 1 2 * 2 5 示例 2: 輸入:c 3 輸出:false 示例 3&…

洛谷 P2919 [USACO08NOV]守護農場Guarding the Farm

題目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety of his valuable milk-cows. He wonders how many guards he will need if he wishes to put one on top of each hill. He has a map supplied as a matrix of int…

iOS 開發一定要嘗試的 Texture(ASDK)

原文鏈接 - iOS 開發一定要嘗試的 Texture(ASDK)(排版正常, 包含視頻) 前言 本篇所涉及的性能問題我都將根據滑動的流暢性來評判, 包括掉幀情況和一些實際體驗 ASDK 已經改名為 Texture, 我習慣稱作 ASDK 編譯環境: MacOS 10.13.3, Xcode 9.2 參與測試機型: iPhone 6 10.3.3, i…

lisp語言是最好的語言_Lisp可能不是數據科學的最佳語言,但是我們仍然可以從中學到什么呢?...

lisp語言是最好的語言This article is in response to Emmet Boudreau’s article ‘Should We be Using Lisp for Data-Science’.本文是對 Emmet Boudreau的文章“我們應該將Lisp用于數據科學”的 回應 。 Below, unless otherwise stated, lisp refers to Common Lisp; in …

鏈接訪問后刷新顏色回到初始_如何使鏈接可訪問(提示:顏色不夠)

鏈接訪問后刷新顏色回到初始Link accessibility is one of the most important aspects of usability. However, designers often dont understand what it takes to make links accessible. Most frequently, they only distinguish links by color, which makes it hard for …

567

567 轉載于:https://www.cnblogs.com/Forever77/p/11519678.html

leetcode 403. 青蛙過河(dp)

一只青蛙想要過河。 假定河流被等分為若干個單元格,并且在每一個單元格內都有可能放有一塊石子(也有可能沒有)。 青蛙可以跳上石子,但是不可以跳入水中。 給你石子的位置列表 stones(用單元格序號 升序 表示&#xff…