pytest(6)-Fixture(固件)
什么是固件
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传递测试数据
普通传递
示例如下:
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函数。
总结
与unittest框架比较,pytest中的Fixture更加丰富,可扩展性更高。
Fixture还有很多更加优雅的用法用于自动化测试项目中,本文只是以最简单的示例进行说明。
pytest(6)-Fixture(固件)的更多相关文章
- pytest.4.Fixture
From: http://www.testclass.net/pytest/fixture/ 我们可以简单的把Fixture理解为准备测试数据和初始化测试对象的阶段. 一般我们对测试数据和测试对象的管 ...
- 『德不孤』Pytest框架 — 12、Pytest中Fixture装饰器(二)
目录 5.addfinalizer关键字 6.带返回值的Fixture 7.Fixture实现参数化 (1)params参数的使用 (2)进阶使用 8.@pytest.mark.usefixtures ...
- pytest测试框架 -- assert断言和fixture固件
一.断言 (1)使用assert语句进行断言 # test_run.py @pytest.mark.assert def test_assert(self): r = requests.get(&qu ...
- pytest 15 fixture之autouse=True
前言 平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了.当用例很多的时候,每次都传这个参数,会比较麻烦.fixture里面有个参数autouse,默认是Fa ...
- pytest 5. fixture之yield实现teardown
前言: 1.前面讲的是在用例前加前置条件,相当于setup,既然有setup那就有teardown,fixture里面的teardown用yield来唤醒teardown的执行 看以下的代码: #!/ ...
- pytest 3.fixture介绍一 conftest.py
前言: 前面一篇pytest2 讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录, ...
- pytest的fixture和conftest
解决问题:用例1需要先登录,用例2不需要登录,用例3需要先登录.很显然这就无法用setup和teardown来实现了,这个时候就可以自定义测试用例的预置条件,比setup灵活很多. 1.fixture ...
- 【Pytest04】全网最全最新的Pytest框架fixture应用篇(2)
一.Fixture参数之params参数可实现参数化:(可以为list和tuple,或者字典列表,字典元祖等) 实例如下: import pytest def read_yaml(): '] @pyt ...
- 【Pytest03】全网最全最新的Pytest框架fixture应用篇(1)
fixtrue修饰器标记的方法通常用于在其他函数.模块.类或者整个工程调用时会优先执行,通常会被用于完成预置处理和重复操作.例如:登录,执行SQL等操作. 完整方法如下:fixture(scope=' ...
随机推荐
- 初识python:scoket 单用户互发消息
实现功能: 启动"服务器".通过"客户端1"连接"服务器",然后互发消息.在此过程中,有"客户端2"连接到"服 ...
- vue3.0+vite+ts项目搭建--初始化项目(一)
vite 初始化项目 使用npm npm init vite@latest 使用yarn yarn create vite 使用pnpm pnpx create-vite 根据提示输入项目名称,选择v ...
- EgLine V0.3—LVGL官方拖拽式UI编辑工具(可导出代码)
** EdgeLine ** 是LVGL官方团队退出的一款拖拽式UI编辑工具,现在还处于测试间断,目前最新版本为v0.3,已经可导出代码. 注意: 使用该软件需要注册lvgl账号,这一步可能需要代理 ...
- Enumy:一款功能强大的Linux后渗透提权枚举工具
Enumy是一款功能强大的Linux后渗透提权枚举工具,该工具是一个速度非常快的可移植可执行文件,广大研究人员可以在针对Linux设备的渗透测试以及CTF的后渗透阶段利用该工具实现权限提升,而Enum ...
- CVE-2020-0786(永恒之黑) GetShell
描述 Microsoft服务器消息块3.1.1(SMBv3)协议处理某些请求的方式中存在一个远程执行代码漏洞,也称为" Windows SMBv3客户端/服务器远程执行代码漏洞". ...
- Solon 开发,四、Bean 扫描的三种方式
Solon 开发 一.注入或手动获取配置 二.注入或手动获取Bean 三.构建一个Bean的三种方式 四.Bean 扫描的三种方式 五.切面与环绕拦截 六.提取Bean的函数进行定制开发 七.自定义注 ...
- 【pwn】攻防世界 pwn新手区wp
[pwn]攻防世界 pwn新手区wp 前言 这几天恶补pwn的各种知识点,然后看了看攻防世界的pwn新手区没有堆题(堆才刚刚开始看),所以就花了一晚上的时间把新手区的10题给写完了. 1.get_sh ...
- Maven 框架结构知识总结
1.maven目录结构 目录 内容 ${basedir} 存放pom.xml和所有子目录 ${basedir}/src/main/java 项目Java代码 ${basedir}/src/main/r ...
- 【C++】指针和函数
指针和函数 标签:c++ 目录 指针和函数 一.基本概念 定义: 指针的定义及使用: 二.指针的相互赋值 三.指针的运算 比较大小: 相减: 加减整数类型变量或者常量: 自增自减: 下标运算符[ ]: ...
- spring事务隔离级别、传播机制以及简单配置
转自 https://blog.csdn.net/zht741322694/article/details/78676964 一.spring支持的事务声明方式1. 编程式事务 当系统需要明确的, ...