pytest的setup和teardown
学过unittest的setup和teardown,前置和后置执行功能。pytest也有此功能并且功能更强大,今天就来学习一下吧。
用例运行级别:
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后
demo1:
import pytest
# 函数式
def setup_function():
print("setup_function") def teardown_function():
print("teardown_function") def setup_module():
print('setup_module') def teardown_module():
print('teardown_module') def test_one():
print("正在执行----test_one") def test_two():
print("正在执行----test_two") def test_three():
print("正在执行----test_three") if __name__ == "__main__":
pytest.main(["-s", "test_api.py"])
结果:

setup_module只开头运行一次,teardown_module只结束运行一次,setup_function每次运行用例都执行一次,teardown_function每次用例结束都执行一次。
demo2:
import pytest
# 函数式
class TestCase():
def setup_class(self):
print("setup_class") def teardown_class(self):
print("teardown_class") def setup_method(self):
print('setup_method') def teardown_method(self):
print('teardown_method') def setup(self):
print('setup') def teardown(self):
print('teardown') def test_one(self):
print("正在执行----test_one") def test_two(self):
print("正在执行----test_two") def test_three(self):
print("正在执行----test_three") if __name__ == "__main__":
pytest.main(["-s", "test_api.py"])
结果:

setup_class只开头执行一次,teardown_class只结束执行一次,setup_method和setup都是在用例执行之前分别执行,setup_method的优先级高于setup,teardown_method和teardown也是同理
运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
pytest的setup和teardown的更多相关文章
- python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)
pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...
- pytest自动化2:测试用例setup和teardown
前言: pytest支持函数和类两种用例方式,针对每种情况都有不同的代码 pytest用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(s ...
- pytest 2.测试用例setup和teardown
之前我写的unittest的setup和teardown,还有setupClass和teardownClass(需要配合@classmethod装饰器一起使用),接下来就介绍pytest的类似于这类的 ...
- pytest文档4-测试用例setup和teardown
前言 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需 ...
- python:pytest中的setup和teardown
原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...
- Pytest学习(三) - setup和teardown的使用
一.前言 从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难. 哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候 ...
- Pytest测试框架(二):pytest 的setup/teardown方法
PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...
- 【pytest】(十二)参数化测试用例中的setup和teardown要怎么写?
还是一篇关于pytest的fixture在实际使用场景的分享. fixture我用来最多的就是写setup跟teardown了,那么现在有一个用例是测试一个列表接口,参数化了不同的状态值传参,来进行测 ...
- Pytest系列(3) - setup和teardown的详细使用
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 用过unittest的童鞋都 ...
随机推荐
- Jenkins持续集成报告列表显示不正确的问题解决
利用robotframework+jenkins对系统进行持续集成,近段时间发现某一系统的Jenkins报告输出列表显示的执行用例数量不对.如图: 经检查发现Configure -- Post-bui ...
- 关于数据源为授权车辆、企业车辆的判断(限foton)
int mode = carInfoService.getCompanyCarMode(companyId); public int getCompanyCarMode(Long companyId) ...
- Linux下的django项目02
3.创建user模型 3.1 创建用户模型user 第一步 django-admin startproject syl 第二 在syl下创建apps文件包并标记根源 cd 到apps下并进行以下步骤 ...
- pip install kaggle 出现 【网络不可达】?
解决办法: pip install kaggle -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
- Java基础知识(三)
一.hashCode 与 equals (重要) 面试官可能会问你:"你重写过 hashcode 和 equals 么,为什么重写equals时必须重写hashCode方法?" 1 ...
- python-sys模块、导入自定义包
import问题:https://zhuanlan.zhihu.com/p/69099185 一.sys模块 sys模块是python自带模块,包含了与Python解释器和它的环境有关的函数.利用 i ...
- leetcode97:maximum -subarray
题目描述 请计算给出的数组(至少含有一个数字)中具有最大和的子数组(子数组要求在原数组中连续) 例如:给出的数组为[−2,1,−3,4,−1,2,1,−5,4], 子数组[−2,1,−3,4,−1,2 ...
- 嗖嗖移动大厅 源代码 Java初级小项目
今天给大家一个比较综合的项目:嗖嗖移动业务大厅.项目功能很多,概括的功能也很全面.吃透了这个项目,你的java基础部分已经非常棒了!!! 一 . 项目概述 技能要求 使用面向对象设计的思想 合 ...
- 极客mysql03
1.务的特性:原子性.一致性.隔离性.持久性 2.多事务同时执行的时候,可能会出现的问题:脏读.不可重复读.幻读 3.事务隔离级别:读未提交.读提交.可重复读.串行化 4.不同事务隔离级别的区别: 读 ...
- rbd-mirror配置指南-单向备份
前言 RBD 的 mirroring 功能将在Jewel中实现的,这个Jewel版本已经发布了很久了,这个功能已经在这个发布的版本中实现了,本来之前写过一篇文章,但是有几个朋友根据文档配置后,发现还是 ...