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.mzph.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…