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的童鞋都 ...
随机推荐
- sql中的字符串拼接
转载自:https://www.cnblogs.com/rainman/p/6203065.html 1. 概述 在SQL语句中经常需要进行字符串拼接,以sqlserver,oracle,mysql三 ...
- puk1521 赫夫曼树编码
Description An entropy encoder is a data encoding method that achieves lossless data compression by ...
- CodeForces 1182E Product Oriented Recurrence
题意 给定五个整数 \(n,f_1,f_2,f_3,c\),其中数列 \(f\) 满足以下递推式: \[f_x=c^{2x-6}f_{x-1}f_{x-2}f_{x-3} \] 求 \(f_n\). ...
- java中常见的六种线程池详解
之前我们介绍了线程池的四种拒绝策略,了解了线程池参数的含义,那么今天我们来聊聊Java 中常见的几种线程池,以及在jdk7 加入的 ForkJoin 新型线程池 首先我们列出Java 中的六种线程池如 ...
- "!"的应用(以C++为例)
1. "!"在条件语句中仅作逻辑判断使用并不影响变量的值 2. "!"在赋值类语句中会改变变量的值(非零->0,0->1)
- glog修改
在写代码的过程中,打log肯定是少不了的,毕竟不能总靠调试来发现问题.log库的选用就很纠结了,成熟的log库非常多,log4cpp.log4cxx.poco.log.boost.log.glog等等 ...
- (三)URI、URL和URN/GET与POST的区别
(一)URI.URL.URN HTTP使用统一资源标识符(Uniform Resource Identifiers,URI)来传输数据和建立连接. URL是一种特殊类型的URI,包含了用于查找某个资源 ...
- 常用的Linux命令,日常收集记录
1.# yum install -y xxxx 解释:install代表往系统中安装一个或者多个软件包:-y 代表回答全部问题为是 2.# ps -ef | grep yum (根据进程名来查看进 ...
- 使用RD Client来远程桌面
使用RD Client来远程桌面 可能你会觉得奇怪,team viewer和向日葵之类的难道不香吗?看起来他们两个都是实现了远程桌面的功能,好像没必要特地用Windows自带的RD Client进行内 ...
- php随机填充字符串内容
public function getStr($str=false){ $poems="从,善,如,登,从,恶,如,崩,已,知,花,意,未,见,其,花,,,已,见,其,花,,,未,闻,花,名 ...