python测试框架之Pytest(二) : 测试用例的前置setup后置teardown用法
目录
- pytest中的前置和后置
- 类内外执行:方法细化级setup/teardown
- 类外执行:模块级(setup_module/teardown_module)
- 类外执行:函数级(setup_function/teardown_function)
- 类内执行:类级(setup_class/teardown_class)
- 类内执行:方法级(setup_method/teardown_method)
- 组合使用
- 有Class的执行顺序:
- 无Class的执行顺序:不执行适用于class内的前置和后置方法
- 使用场景
pytest中的前置和后置
pytest比较强大,提供了不仅仅一种方法的前置和后置:
– 模块级(setup_module/teardown_module)开始于横块始末,全局的
– 函数级(setup_function/teardown_function)只对函数用例生效
– 类级(setup_class/teardown_class)只在类中前后运行一次
–方法级(setup_method/teardown_method)开始于方法始末
–(setup/teardown)运行在调用方法前后
类内外执行:方法细化级setup/teardown
setup/teardown:无论是类内还是类外,每个方法都会执行1次。
setup: 方法运行前执行
teardown:方法运行完才执行
# file_name: test_setup.py
import pytest
def setup():print("...类外setup...")
def test_create():print("类外test_create")
def test_view():print("类外test_view")
class TestSetupClass:def setup(self):print("...类内setup...")def test_create(self):print("类内test_create")def test_view(self):print("类内test_view")def teardown(self):print("...类内teardown...")
def teardown():print("...类外teardown...")
if __name__ == '__main__':pytest.main(['-vs', 'test_setup.py'])
执行结果如下:类外类内方法的都有执行前置和后置,每个方法分别执行1次。
备注:也可以通过在fixture函数中通过yield实现setup和teardown功能
类外执行:模块级(setup_module/teardown_module)
setup_module/teardown_module:只有前置后置方法在class外部时,才会被执行。整个.py文件只执行1次。适合.py存在多个class时使用。setup_module: 在.py文件的第一个class前执行。teardown_module:在.py文件的最后一个class后执行。
测试代码如下:class内外同时存在模块级方法
# file_name:test_setup_module.py
import pytest
def setup_module():print("...类外 setup_module...")
class TestModuleClass:def setup_module(self):print("...类内 setup_module...")def test_create(self):print("测试用例test_create")def test_view(self):print("测试用例test_view")def teardown_module(self):print("...类内 teardown_module...")
def teardown_module():print("...类外 teardown_module...")
if __name__ == '__main__':pytest.main(['-vs')
执行结果如下:只执行class外的前置后置方法,只执行1次。第1个class TestModuleClass1前执行前置,最后1个class TestModuleClass2之后执行后置。
类外执行:函数级(setup_function/teardown_function)
setup_function/teardown_function:只有类外部的方法才会执行。每个方法执行一次。setup_function: 类外部的方法前执行。teardown_function: 类外部的方法后执行。
# file_name:test_setup_function.py
import pytest
def setup_function():print("...类外setup_function...")
def test_create():print("类外测试用例test_create")
def test_view():print("类外测试用例test_view")
class TestFunctionClass:def setup_function(self):print("...类内setup_function...")def test_create(self):print("测试用例test_create")def test_view(self):print("测试用例test_view")def teardown_function(self):print("...类内teardown_function...")
def teardown_function():print("...类外teardown_function...")
if __name__ == '__main__':pytest.main(['-vs', 'test_setup_function.py'])
执行结果如下:只执行class外的方法,类外方法test_create/test_view分别执行1次:方法前执行setup_function,方法后执行teardown_function。
类内执行:类级(setup_class/teardown_class)
setup_class/teardown_class:只有方法在class内部时,才可以执行。class内只运行1次。setup_class:在第一个method运行前执行。teardown_class:在最后一个method运行前执行。
# file_name:test_setup_class.py
import pytest
def setup_class():print("...类外 setup_class...")
class TestClassClass:def setup_class(self):print("...类内 setup_class...")def test_create(self):print("测试用例test_create")def test_view(self):print("测试用例test_view")def teardown_class(self):print("...类内 teardown_class...")
def teardown_class():print("...类外 teardown_class...")
if __name__ == '__main__':pytest.main(['-vs', 'test_setup_class.py'])
执行结果如下:只执行class内的方法,第一个方法test_create前执行setup_class,最后一个方法test_view后执行teardown_class。
类内执行:方法级(setup_method/teardown_method)
setup_method/teardown_method:只有方法在class内部时,才可以执行。class内运行多次。setup_method: 每个method前执行一次。teardown_method: 每个method后执行一次。在method运行的前后执行。,每个method都会运行1次。
# file_name:test_setup_method.py
import pytest
def setup_method():print("...类外 setup_method...")
class TestMethodClass:def setup_method(self):print("...类内 setup_method...")def test_create(self):print("测试用例test_create")def test_view(self):print("测试用例test_view")def teardown_method(self):print("...类内 teardown_method...")
def teardown_method():print("...类外 teardown_method...")
if __name__ == '__main__':pytest.main(['-vs', 'test_setup_method.py'])
执行结果如下:只执行class内的方法,执行了2次。test_create/test_view:方法前执行setup_method,方法执行setup_method,分别执行了1次。
组合使用
由于Python语法规范,.py文件的class可有可无,故分2种情况来演示说明。
有Class的执行顺序:
setup_module => setup_function => 类外setup => 类外测试用例
=> setup_class => setup_method => 类内setup => 类内测试用例先执行class外部的方法,再按顺序执行class内方法。
import pytest
def setup_module():print("类外setup_module")
def setup_function():print("类外setup_function")
def setup():print("类外setup")
def test_create():print("类外测试用例test_create")
def test_view():print("类外测试用例test_view")
class TestClass_1:def setup_class(self):print("类内setup_class")def setup_method(self):print("类内setup_method")def test_create_1(self):print("测试用例test_create_1")def test_view_1(self):print("测试用例test_view_1")def teardown_method(self):print("类内teardown_method")def teardown_class(self):print("类内teardown_class")
class TestClass_2:def test_create_2(self):print("测试用例test_create_2")def test_view_2(self):print("测试用例test_view_2")
def teardown():print("类外teardown")
def teardown_function():print("类外teardown_function")
def teardown_module():print("类外teardown_module")
if __name__ == '__main__':pytest.main(['-vs', 'test_all_setup_class.py'])
执行结果如下:
无Class的执行顺序:不执行适用于class内的前置和后置方法
setup_module => setup_function => setup => 测试用例未执行:setup_class、setup_method
import pytest
def setup_module():print("setup_module")
def setup_function():print("setup_function")
def setup():print("setup")
def setup_class():print("setup_class")
def setup_method():print("setup_method")
def test_create():print("测试用例test_create")
def test_view():print("测试用例test_view")
def teardown_method():print("teardown_method")
def teardown_class():print("teardown_class")
def teardown():print("teardown")
def teardown_function():print("teardown_function")
def teardown_module():print("teardown_module")
if __name__ == '__main__':pytest.main(['-vs', 'test_all_setup.py'])
执行结果如下:没有class时,默认不执行适用在class内的前置和后置方法
使用场景
若是.py文件中没有定义class,可以使用setup_module、setup_function、setup,它们的优先级别是:setup_module > setup_function > setup。
setup_function 与 setup_method 都是方法级前置,setup_method适用于class内。
setup_function 与 setup都会在每个方法前执行1次,所以只使用其中1个即可。
setup_function 优于setup,但是setup没有类内外的限制,建议方法级别使用setup。
setup_module: .py文件中只执行1次,最先执行的前置,适合统一登录、数据库连接等。
setup: 每个方法前执行,适合测试用例的数据准备、单独登录等。
python测试框架之Pytest(二) : 测试用例的前置setup后置teardown用法相关推荐
- Python测试框架之pytest详解
目录 前言 1.pytest安装 2.Pytest的setup和teardown函数 3.Pytest配置文件 4 Pytest常用插件 4.1 前置条件: 4.2 Pytest测试报告 5.pyte ...
- 全功能Python测试框架:pytest
python通用测试框架大多数人用的是unittest+HTMLTestRunner,这段时间看到了pytest文档,发现这个框架和丰富的plugins很好用,所以来学习下pytest. pytest ...
- Python测试框架pytest(04)fixture - 测试用例调用fixture、fixture传递测试数据
Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 Python测试框架pytest(02)PyCharm设 ...
- python测试框架untest_Python测试框架之unittest和pytest
目前搜狗商城接口测试框架用的是unittest+HTMLTestRunner,case数有1097条,目前运行一次自动化测试,时长约为30分钟,期望控制在10分钟或者更短的时间内.近期打算重新优化框架 ...
- Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture
Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 Python测试框架pytest(02)PyCharm设 ...
- Python测试框架Pytest的基础入门
Pytest简介 Pytest is a mature full-featured Python testing tool that helps you write better programs.T ...
- Python测试框架pytest(03)setup和teardown
Python测试框架pytest系列可以查看下列 Python测试框架pytest(01)简介.安装.快速入门_编程简单学的博客-CSDN博客 Python测试框架pytest(02)Py ...
- gtest测试框架使用详解_【python】新手小白必看,教你如何使用全功能Python测试框架 - python秋枫...
大家好,我是在升职加薪道路上越奋斗头发越少的阿茅. 今天来跟想入门还徘徊在门外的小白们聊一聊 1.安装和简单使用 2.配置文件 3.断言 一. 第1步 (安装和简单使用) pytest是一个非常成熟的 ...
- Python自动化测试框架之Pytest教程【让你小鸡变老鹰】
Pytest pytest是一个非常成熟的全功能的Python测试框架,主要有以下几个特点: · 简单灵活,容易上手 · 支持参数化 · 能够支持简单的单元测试和复杂的功能测试,还可以用来做sele ...
最新文章
- 荐读 | 9篇近期社会化推荐论文
- xp怎样安装android-studio,Xposed 框架的安装
- android 自定义表情包,android基于环信的聊天和表情自定义
- mysql 时间段在不在另外的时间段中_你知道自来水一天中哪个时间段最脏、最具毒性吗?记住这几点避开致命自来水...
- Python学习笔记简单数据类型之数字
- [转]本地图片预览二三事
- Vue + Spring Boot 项目实战(一):项目简介
- c++无法启动程序,系统找不到指定文件的处理方法
- Vistual Studio Code中Markdown使用数学公式
- 计算机桌面没有有了怎么添加,电脑里没有便签小工具怎么办?办公电脑上怎么添加一款方便记事的桌面便签软件...
- 成熟的 XMOS 方案 ——「 XMOS 麦克风阵列 」
- 用h5的canvas实现动画的泡沫
- 鼻子怎么画?素描鼻子的详细画法!
- 小学语文三年级阅读理解专项练习附答案~高尔基和孩子们
- OpenCV图形图像击中击不中HITMISS变换处理基础知识
- linux驱动学习(七) ioctl中的cmd和_IO() , _IOR() , IOW() ,_IOWR() 以及_IOC_NR()的关系
- 三星i8268android4.4,三星I8268(移动版)
- 《惢客创业日记》2018.12.19(周三)创业从0到1的10个阶段(六)
- SqlServer日期格式
- 《走出软件作坊》登录武汉光谷书城!