第六:Pytest中的setup/teardown
- 简介
学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次。
当然还有更高级一点的setupClass和teardownClass,需配合@classmethod装饰器一起使用,在做selenium自动化的时候,它的效率尤为突出,可以只启动一次浏览器执行多个用例。
pytest框架也有类似于setup和teardown的语法,并且还不止这四个
2.用例运行级别
2.1.模块级(setup_module/teardown_module)开始于模块始末,全局的
2.2.函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
2.3.类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
2.4.方法级(setup_method/teardown_method)开始于方法始末(在类中)
3.函数式
3.1.setup_function/teardown_function (每个用例开始和结束时调用一次)
test_bjhg_class1.py
#content of test_bjhg_class1.py
import pytest
#函数式
def setup_function():print("setup_function:每个用例开始前都会执行")def teardown_function():print("teardown_function:每个用例结束后都会执行")def test_one():print("正在执行----test_one")x = "this"assert 'h' in xdef test_two():print("正在执行----test_two")x = "hello"assert hasattr(x, 'check')def test_three():print("正在执行----test_three")a = "hello"b = "hello world"assert a in bif __name__ == "__main__":pytest.main(["-s", "test_bjhg_class1.py"])
3.1.1.运行结果
3.2.setup_module/teardown_module(所有用例开始和结束时调用一次)
test_bjhg_class2.py
#content of test_bjhg_class2.py
import pytest
#函数式def setup_module():print("setup_module:整个.py模块只执行一次")print("比如:所有用例开始前只打开一次浏览器")def teardown_module():print("teardown_module:整个.py模块只执行一次")print("比如:所有用例结束只最后关闭浏览器")def setup_function():print("setup_function:每个用例开始前都会执行")def teardown_function():print("teardown_function:每个用例结束前都会执行")def test_one():print("正在执行----test_one")x = "this"assert 'h' in xdef test_two():print("正在执行----test_two")x = "hello"assert hasattr(x, 'check')def test_three():print("正在执行----test_three")a = "hello"b = "hello world"assert a in bif __name__ == "__main__":pytest.main(["-s", "test_bjhg_class2.py"])
3.2.1.运行结果
3.3.类和方法:setup_class和teardown_class等价于unittest里面的setupClass和teardownClass
test_bjhg_class3.py
#content of test_bjhg_class3.py
import pytest
#类和方法
class TestCase():def setup(self):print("setup: 每个用例开始前执行")def teardown(self):print("teardown: 每个用例结束后执行")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 test_one(self):print("正在执行----test_one")x = "this"assert 'h' in xdef test_two(self):print("正在执行----test_two")x = "hello"assert hasattr(x, 'check')def test_three(self):print("正在执行----test_three")a = "hello"b = "hello world"assert a in bif __name__ == "__main__":pytest.main(["-s", "test_bjhg_class3.py"])
3.3.1.运行结果:这里setup_method和teardown_method的功能和setup/teardown功能是一样的,一般二者用其中一个即可。
3.4.函数和类混合:如果一个.py的文件里面既有函数用例又有类和方法用例,运行顺序又是怎样的呢?
test_bjhg_class4.py
#content of test_bjhg_class4.py
import pytest
#类和方法
def setup_module():print("setup_module:整个.py模块只执行一次")print("比如:所有用例开始前只打开一次浏览器")def teardown_module():print("teardown_module:整个.py模块只执行一次")print("比如:所有用例结束只最后关闭浏览器")def setup_function():print("setup_function:每个用例开始前都会执行")def teardown_function():print("teardown_function:每个用例结束前都会执行")def test_one():print("正在执行----test_one")x = "this"assert 'h' in xdef test_two():print("正在执行----test_two")x = "hello"assert hasattr(x, 'check')class TestCase():def setup_class(self):print("setup_class:所有用例执行之前")def teardown_class(self):print("teardown_class:所有用例执行之前")def test_three(self):print("正在执行----test_three")x = "this"assert 'h' in xdef test_four(self):print("正在执行----test_four")x = "hello"assert hasattr(x, 'check')if __name__ == "__main__":pytest.main(["-s", "test_bjhg_class4.py"])
3.4.1.运行结果:setup_module/teardown_module的优先级是最大的,然后函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉
第六:Pytest中的setup/teardown相关推荐
- python:pytest中的setup和teardown
原文:https://www.cnblogs.com/peiminer/p/9376352.html 之前我写的unittest的setup和teardown,还有setupClass和teardow ...
- pytest中前置setup和后置teardown的使用方法
前言 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次. 当然还有更高级一点的setupClass和teardownClass,需 ...
- 5.pytest中setup和teardown
文章目录 前言 函数级别 模块级别 类级别 前言 用过unittest的童鞋都知道,有两个前置方法,两个后置方法:分别是 setup() setupClass() teardown() teardow ...
- pytest中setup和teardown
参考博客: pytest 2.测试用例setup和teardown以及使用@pytest.fixture()装饰器来实现部分用例的前后置,param参数化,ids(不显示unicode编码),name ...
- 三、pytest接口自动化之pytest中setup/teardown,setup_class/teardown_class讲解
pytest框架实现的前后置的处理(固件,夹具),很多种方式,常见的三种. 一.setup/teardown,setup_class/teardown_class 为什么需要这些功能? Class T ...
- python pytest setupclass_简单了解pytest测试框架setup和tearDown
pytest的setup与teardown 1)pytest提供了两套互相独立的setup 与 teardown和一对相对自由的setup与teardown 2)模块级与函数级 模块级(setup_m ...
- 【pytest】(六) pytest中fixture的使用
上篇文章中提到了,在pytest中的fixture可以完成unitest中setUp.tearDown.今天就来详细看一下. 一.pytest中的fixture是什么 为可靠的和可重复执行的测试提供固 ...
- Pytest setup teardown
目录 如何实现xunit样式setup\teardown 模块级setup/teardown 类级别setup/teardown 方法和功能级别setup/teardown 如何实现xunit样式se ...
- Pytest测试框架(二):pytest 的setup/teardown方法
系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...
最新文章
- WinXP/2000操作系统自动关机的实现
- 新版MySQL8.0.22JDBC连接数据库常见问题解决
- uniny 物体运动到一个点停止_Unity3D中的逐点运动
- mybatis一对多关联查询_Mybatis 一对一、一对多的关联查询 ?
- 字节跳动(今日头条),战斗力为何如此凶猛?| 畅言
- 《UG NX10中文版完全自学手册》——2.4 布局
- python数据库操作sqlite_用Python进行SQLite数据库操作
- mapxtreme java_MapXtreme Java
- 小程序 VS APP优缺点
- 双路CPU笔记本计算机,什么是双路cpu cpu双路什么意思 - 云骑士一键重装系统
- IF:4+ 铁代谢和免疫相关基因标记预测三阴性乳腺癌的临床结局和分子特征
- 使用Echarts在前端页面绘制地图
- 神仙打架!清华公布2020特奖候选人名单,有人三篇顶会一作!还有人...
- 最新Lua网络验证系统+lua代码/PHP后端开发
- raspberry pi cm3+的i2c使用的问题调试
- WKWebView白屏问题
- 如何快速推广引流?利用微博截流疯狂获取流量
- Locating Elements
- 大数据之当传统产业遭遇互联网
- 如何才能做一个淡定从容的人呢?