pytest测试框架系列 - setup和teardown的详细讲解
简介
前面使用过unittest的都知道,setup和teardown是用来处理用例的前置工作和清理工作,其中还有setupclass和teardownclass是保证执行所以的用例都只执行1次前置和后置,使用起来非常方便,对于强大的pytest测试框框,给我们提供了10中类似方法。
- 模块级别:setup_module、teardown_module
- 函数级别:setup_function、teardown_function
- 类级别:setup_class、teardown_class
- 类方法级别:setup_method、teardown_method
- 函数和方法级别:setup、teardown
详细用法讲解
模块级别:setup_module、teardown_module
说明:该方法表示只能类外面执行用例过程中,只执行1次。相当于unittest中的setupclass和teardownclass方法
示例如下:
# _*_coding:utf-8 _*_# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】import pytestdef setup_module():print('这是setup_module的前置')def teardown_module():print('这是setup_module的后置')def test_01():print('我是test_01用例')class TestDemo:def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()
命令行输入: pytest -s test_demo01.py
执行结果如下:
函数级别:setup_function、teardown_function
说明:该方法表示在类外面执行用例过程中,每次都会执行前置和后置。
示例如下:
# _*_coding:utf-8 _*_# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytestdef setup_function():print('这是 setup_function 的前置')def teardown_function():print('这是 teardown_function 的后置')def test_01():print('我是类外面的 test_01 用例')class TestDemo:def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()
命令行输入: pytest -s test_demo01.py
执行结果如下:
类级别:setup_class、teardown_class
说明:该方法表示在类中执行测试用例前,只执行1次测试前置和测试后置,注意:放在类外面不生效
示例如下:
# _*_coding:utf-8 _*_# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytestdef test_01():print('我是类外面的 test_01 用例')class TestDemo:def setup_class(self):print("我是 setup_class 方法")def teardown_class(self):print("我是 teardown_class 方法")def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()
命令行输入: pytest -s test_demo01.py
执行结果如下:
方法级别:setup_method、teardown_method
说明:该方法表示在类中每次执行测试用例前,测试前置和测试后置都会执行一次,注意:放在类外面不生效
示例如下:
# _*_coding:utf-8 _*_# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytestdef test_01():print('我是类外面的 test_01 用例')class TestDemo:def setup_method(self):print("我是 setup_method 方法")def teardown_method(self):print("我是 teardown_method 方法")def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()
命令行输入: pytest -s test_demo01.py
执行结果如下:
函数和方法级别:setup、teardown
说明:该方法这个可以在类中使用,也可以在类外进行使用,大家最熟悉
示例如下:
# _*_coding:utf-8 _*_# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytestdef setup():print("我是类外面 setup 方法")def teardown():print("我是类外面 teardown 方法")def test_01():print('我是类外面的 test_01 用例')class TestDemo:def setup(self):print("我是类里面 setup 方法")def teardown(self):print("我是类里面 teardown 方法")def test_one(self):print("我是类里面方法 test_one")def test_two(self):print("我是类里面方法 test_two")if __name__ == '__main__':pytest.main()
命令行输入: pytest -s test_demo01.py
执行结果如下:
多个方法组合使用
说明:直接查看示例,查看各个方法执行优先级,非常重要
示例如下:
# _*_coding:utf-8 _*_# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytestdef setup_module():print("我是类外面的 setup_module 方法")def teardown_module():print("我是类外面的 teardown_module 方法")def setup_function():print("我是类外面的 setup_function 方法")def teardown_function():print("我是类外面的 teardown_function 方法")def setup():print("我是类外面 setup 方法")def teardown():print("我是类外面 teardown 方法")def test_one():print("我是类外面的 test_one 用例")def test_two():print("我是类外面的 test_two 用例")class TestDemo():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 用例")if __name__ == '__main__':pytest.main()
命令行输入: pytest -s test_demo01.py
执行结果如下:
我们根据执行结果看见方法执行顺序
- setup_module、teardown_module 整个测试只执行一次
- setup_function、teardown_function 类外面方法每个用例都执行
- setup_class、teardown_class 类里面只执行一次
- setup_method、teardown_method 类里面每个用例都执行
- setup、teardown 类里面和外面每个用例都执行
优先级为:
- setup_module>setup_function>setup>用例>teardown>teardown_function>teardown_module
- setup_module>setup_class>setup_method>setup>用例>teardown>teardown_method>teardown_class>teardown_module
以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!
如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章
pytest测试框架系列 - setup和teardown的详细讲解相关推荐
- pytest测试框架系列 - pytest 断言assert使用
前言 测试用例的预期结果是用例不可缺少的一部分,那么断言就是自动化测试不可缺少的一步,一个没有断言的用例,自动化测试的就没有意义了.那什么是断言呢? 简单来讲就是实际结果和期望结果去对比,符合预期那就 ...
- Pytest测试框架(二):pytest 的setup/teardown方法
系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...
- Pytest测试框架中,setup和teardown函数的作用及使用方法
作为一名Python开发人员,您一定知道Pytest是一个广泛使用的Python测试框架. 它具有易用性和灵活性,可以帮助您快速编写测试用例,并且能够支持包括单元测试.集成测试.功能测试等多种测试类型 ...
- 九、Pytest框架 — Pytest框架的setUp()和tearDown()函数
###文章内容有配套的学习视频和笔记都放在了文章末尾### 1.setUp()和tearDown()函数介绍 之前学过Unittest测试框架,知道前置setup()函数和后置teardown()函数 ...
- Pytest测试框架(五):pytest + allure生成测试报告
系列文章目录 Pytest测试框架(一):pytest安装及用例执行 Pytest测试框架(二):pytest 的setup/teardown方法 Pytest测试框架(三):pytest fixtu ...
- Python编程必不可少的pytest测试框架
进行编程测试重要的是为了更高效的完成功能的实现. pytest是基于unittest实现的第三方测试框架,比 unittest 更加的简洁.高效,并且可以完美兼容 unittest 的测试代码,无需对 ...
- Pytest 测试框架——数据驱动
引言 前面已经和大家介绍过 Unittest 测试框架的数据驱动框架 DDT,以及其实现原理.今天和大家分享的是 Pytest 测试框架的数据驱动,Pytest 测试框架的数据驱动是由 pytest ...
- Python测试框架pytest(03)setup和teardown
Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 Python测试框架pytest(02)Py ...
- python pytest setupclass_简单了解pytest测试框架setup和tearDown
pytest的setup与teardown 1)pytest提供了两套互相独立的setup 与 teardown和一对相对自由的setup与teardown 2)模块级与函数级 模块级(setup_m ...
最新文章
- 构造函数(constructor)与原型链(prototype)关系
- linux启动spark命令,在linux上安装spark
- [学习笔记]51单片机
- 【业务知识】企业数字档案馆总体架构图
- Cpp / 通用引用、引用折叠与完美转发问题
- CentOS7.3的基本配置(建议学习用)
- 通过 Apache Ant 来运行 Tomcat
- 设计模式-第四篇之代理模式
- WPS2019专业版,最新免费高级版
- SpringCloud服务器服务注册在Euraka上,访问IP地址问题
- itext设置字体间距_微信公众号文章字体怎么修改?行间距、字间距一般设置多少?...
- 计算机网络-应用层笔记
- 【3】基于OpenCV—Python绘制图片三维空间显示图
- 交叉连接(笛卡尔积)
- shp系列(七)——利用C++进行Shx文件的写(创建)
- win10禁用USB恢复USB
- KNN最近邻算法理解
- icq蓝牙_使用WhizBase发送ICQ消息
- 关于 intel 82583v 在 server2012 下安装问题
- CloudComparePCL 剔除点云中的重复点