关于自动化测试框架pytest的Fixture固件
作者:阿里测试君 发布时间:2023-10-16 00:02:50
什么是固件
Fixture 翻译成中文即是固件的意思。它其实就是一些函数,会在执行测试方法/测试函数之前(或之后)加载运行它们,常见的如接口用例在请求接口前数据库的初始连接,和请求之后关闭数据库的操作。
我们之前在APP UI自动化系列中已经介绍过 unittest 的相关测试固件,如setup
、teardown
等。而 pytest 中提供了功能更加丰富的Fixture
,用于实现setup
、teardown
功能。
定义方式
使用@pytest.fixture()
进行定义,简单示例如下:
import pytest
@pytest.fixture()
def before():
print("连接数据库")
调用方式
调用单个fixture函数
方式一,使用fixture函数名作为参数
import pytest
@pytest.fixture()
def before():
print("连接数据库")
# 调用before
def test_01(before):
print("执行test_01")
方式二,使用
@pytest.mark.usefixtures('fixture函数名')
装饰器
import pytest
@pytest.fixture()
def before():
print("连接数据库")
# 调用before
@pytest.mark.usefixtures('before')
def test_01():
print("执行test_01")
方式三,使用
autouse
参数自动执行fixture
函数
import pytest
# fixture函数定义的时候使用autouse参数,作用域范围内的测试用例会自动调用该fixture函数
@pytest.fixture(autouse=True)
def before():
print("连接数据库")
# 自动调用before
def test_01():
print("执行test_01")
三种方式调用后的结果都如下:
我们可以看到,先执行了fixture
函数,再执行测试函数。
调用多个fixture函数
import pytest
@pytest.fixture()
def before():
print("连接数据库")
@pytest.fixture()
def before_s():
print("初始化数据")
def test_01(before, before_s):
print("执行test_01")
调用多个 fixture 函数时,由前至后依次执行,所以test_01()
调用时先执行before
,再执行before_s
。
对fixture函数重命名
定义fixture
函数时,可以利用name
参数进行重命名,方便用于调用,示例如下:
import pytest
@pytest.fixture(name='db')
def connect_order_db():
print("连接数据库")
def test_01(db):
print("执行test_01")
使用fixture传递测试数据
在执行完fixture
函数后,有时需要将该fixture
中得到到某些数据传递给测试函数/测试方法,用于后续的执行。
fixture
中提供普通传递和参数化传递两种数据传递方式。
普通传递
示例如下:
import pytest
@pytest.fixture()
def before():
print("连接数据库")
return "连接成功!"
def test_01(before):
print("执行test_01")
assert before == "连接成功!"
注意,如果自定义的fixture
函数有返回值,需要使用上面说的方式一调用才能获取fixture
函数的返回值并传入测试函数中,方式二就无法获取返回值。
参数化传递
对fixture
函数进行参数化时,需要使用参数params
,并且需要传入参数request
,简单示例如下:
import pytest
test_params = [1, 2, 0]
@pytest.fixture(params=test_params)
def before(request):
result = request.param
return result
def test_02(before):
print("执行test_02")
assert before
if __name__ == '__main__':
pytest.main()
执行结果:
可以看到,因为所调用的fixture函数进行了参数化,虽然只有一个测试函数但执行了3次。
conftest.py
上面我们举的例子都是把fixture函数放在测试用例模块里面,但如果很多测试模块需要引用同一个fixture函数怎么办,这是时候就需要把它放在命名为conftest
的模块里,这样同级或以下目录中的测试用例便能调用这些自定义的fixture函数。
例如,有如下目录:
├─testcase
│ │
│ ├─test_module_01
│ │ test_case_1.py
│ │ test_case_2.py
│ │
│ ├─test_module_02
│ │ test_case_3.py
test_module_01 中的test_case_1.py
与test_case_2.py
都需要调用同一个 fixture 函数,那么我们只需要在 test_module_01 中新建conftest.py
并编写这个fixture
函数即可,示例如下:
├─testcase
│ │
│ ├─test_module_01
│ │ conftest.py
│ │ test_case_1.py
│ │ test_case_2.py
│ │
│ ├─test_module_02
│ │ test_case_3.py
conftest.py
:
import pytest
@pytest.fixture(autouse=True)
def before():
print("连接数据库")
test_case_1.py
:
def test_01():
print("执行test_01")
test_case_2.py
:
def test_02():
print("执行test_02")
这样,执行这两个模块的测试用例时会自动先去调用conftest.py
中的before()
函数。
假设 test_module_02 中的 test_case_3.py 也需要调用这个before()
函数,那么这个时候我们就需要在上一层即 testcase 中新建conftest.py
并编写这个before()
函数,才能在 test_case_3.py 中调用,如下:
├─testcase
│ │ conftest.py
│ │
│ ├─test_module_01
│ │ conftest.py
│ │ test_case_1.py
│ │ test_case_2.py
│ │
│ ├─test_module_02
│ │ test_case_3.py
conftest.py
只作用于同级
或以下
目录中的测试模块,且需要注意,当以下
层级中存在了另一个conftest.py
,那么以下层级将由另一个conftest.py
文件接管。
作用域
pytest 的 fixture 作用域分session
、module
、class
、function
四个级别。在定义 fixture 函数的时候通过scope
参数指定作用范围,默认为function
。
session
,每次会话执行一次module
,每个测试模块执行一次class
,每个测试类执行一次function
,每个测试方法执行一次
注意,对于单独定义的测试函数,class、function 都会起作用,可以从下列示例中看出来。
测试目录结构如下:
├─apiAutoTest
│ │ run.py
│ │
│ ├─testcase
│ │ │ conftest.py
│ │ │
│ │ ├─test_module_02
│ │ │ │ conftest.py
│ │ │ │ test_case_3.py
│ │ │ │ test_case_4.py
其中conftest.py
代码如下:
import pytest
@pytest.fixture(scope="session", autouse=True)
def session_fixture():
print("这是一个作用于session的fixture")
@pytest.fixture(scope="module", autouse=True)
def module_fixture():
print("这是一个作用于module的fixture")
@pytest.fixture(scope="class", autouse=True)
def class_fixture():
print("这是一个作用于class的fixture")
@pytest.fixture(scope="function", autouse=True)
def function_fixture():
print("这是一个作用于function的fixture")
test_case_3.py
代码如下:
import pytest
class TestOrder:
def test_a(self):
print("test_a")
def test_b(self):
print("test_b")
def test_c():
print("test_c")
test_case_4.py
代码如下:
def test_e():
print("test_e")
run.py
代码如下:
import pytest
if __name__ == '__main__':
pytest.main(["-s"])
运行run.py
,结果如下:
collected 4 items
testcase\test_module_02\test_case_3.py
这是一个作用于session的fixture
这是一个作用于module的fixture
这是一个作用于class的fixture
这是一个作用于function的fixture
test_a
.这是一个作用于function的fixture
test_b
.这是一个作用于class的fixture
这是一个作用于function的fixture
test_c
.
testcase\test_module_02\test_case_4.py
这是一个作用于module的fixture
这是一个作用于class的fixture
这是一个作用于function的fixture
test_e
.
============================== 4 passed in 0.04s ==============================
从结果可以看出来:
作用于
session
的fixture函数只在所有测试用例执行之前调用了一次作用于
module
的fixture函数在每个测试模块执行之前调用了一次作用于
class
的fixture函数在每个测试类执行之前调用了一次作用于
function
的fixture函数在每个测试方法/测试函数执行之前调用了一次
注意,在定义的测试函数(如test_c()
、test_e()
)执行之前也会调用scope=class的fixture函数。
来源:https://blog.csdn.net/m0_60054525/article/details/126592717


猜你喜欢
- 一、前言运算符重载:为运算符定义方法所谓重载,就是赋予新的含义同一个运算符可以有不同的功能二、重载作用让自定义的实例像内建对象一样进行运算符
- 关于@property装饰器在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问。那么为什么要这样做呢?因为@
- git-commit-id-maven-plugin插件,会根据当前分支的版本号生成一个git.properties文件。git.prope
- 一、停止数据库服务打开sqlserver自带的配置管理器,停止数据库服务。二、打开cmd窗口,执行命令(以管理员运行)以单用户模式启动sql
- 本文实例讲述了python中@property和property函数常见使用方法。分享给大家供大家参考,具体如下:1、基本的@propert
- cmake-2.8.3.tar.gzmysql-5.5.8.tar.gz一,cmake-2.8.3的安装:tar -zxf cmake-2.
- 目录技术背景加速场景基于Numba的GPU加速总结概要技术背景GPU加速是现代工业各种场景中非常常用的一种技术,这得益于GPU计算的高度并行
- 做个性休闲类项目课程材料,对这方面要求多一些,要总结方法、手法、想法等等,头大了;这里总结了一个做个性字体设计的方法,分享一下;方法是比较简
- 1. PHP入侵检测系统PHP IDS(即PHP-入侵检测系统)是一套易于使用、结构良好、速度出色且专门面向PHP类Web应用程序的先进安全
- 关于asp缓存函数,类什么的,在网上可以说笔笔皆是,为啥我要不辞辛苦去写一个呢?大概看了下,各有各的优点吧,可是大部分好像不可以缓存数据额,
- 有一个ssqdatav2数据,要找到其中的深圳,并且替换成圳。因为收集到的数据出现了错误,本来只有省份简写的地方却出现了深圳。如何找到DF中
- 本文实例讲述了golang简单位运算。分享给大家供大家参考,具体如下:// http://play.golang.org/p/idG7Ri_
- 一、前言该测试功能是Linux产测软件的一个子功能,主要涉及:140行代码PySide2的Event、信号和槽、QLabel,QWidget
- 前言Go语言是Google内部主推的语言,它作为一门全新的静态类型开发语言,与当前的开发语言相比具有许多令人兴奋不已的新特性。专门针对多处理
- 新建server.jsyarn init -yyarn add express nodemon -Dvar express = requir
- 目录一、Python 中的作用域规则和嵌套函数二、定义闭包函数三、何时使用闭包?四、总结一、Python 中的作用域规则和嵌套函数每当执行一
- openpyxl特点openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件,xls和xlsx之间转换容
- 设置表名为中文1.设置Models.py文件class Post(models.Model): name = models.CharFiel
- 一、安装并配置 JMeter下载官网下载,下载二进制的这个 zip配置环境变量然后解压到你喜欢的位置,配置环境变量,新建一个 JMETER_
- 在昨天关于substring的blog中有如下一段代码:也许你已经发现,在Python 3中其实有办法只用一行完成函数:>>&g