pytest 固件及 Fixture
pytest 固件
固件用于执行前的初始化参数、执行后的清理动作。
| 类型 | 规则 |
|---|---|
| setup_module/teardown_module | 全局模块级——模块运行前/后运行(只运行一次) |
| setup_function/teardown_function | 函数级——每个函数用例运行前/后运行 |
| setup_class/teardown_class | 类级——每个class运行前/后运行(只运行一次) |
| setup(setup_method)/teardown(teardown_method) | 方法级——类中每个方法用例执行前/后运行 |
例子:一个module,两个函数,两个类,每个类两个方法
def setup_module():
print("初始化:setup_module")
def teardown_module():
print("清理:teardown_module")
def setup_function():
print("初始化:setup_function")
def teardown_function():
print("清理:teardown_function")
def test_f():
print("--------------test_f")
class Test01:
def setup_class(self):
print("初始化:setup_class1")
def teardown_class(self):
print("清理:teardown_class1")
def setup_method(self):
print("初始化:setup_method1")
def teardown_method(self):
print("清理:teardown_method1")
def test_d(self):
print("--------------test_d")
def test_c(self):
print("--------------test_c")
class Test02:
def setup_class(self):
print("初始化:setup_class2")
def teardown_class(self):
print("清理:teardown_class2")
def setup_method(self):
print("初始化:setup_method2")
def teardown_method(self):
print("清理:teardown_method2")
def test_b(self):
print("--------------test_b")
def test_a(self):
print("--------------test_a")
def test_e():
print("--------------test_e")
测试用例根据代码书写顺序执行:

pytest 之 Fixture
如果我们只希望部分测试用例执行某个固件,通过 setup 和 teardown 是实现不了的;但是,通过 fixture 就可以根据需要自定义测试用例的前置、后置操作。
调用方法
使用 @pytest.fixture() 标记,测试用例引用该方法。
import pytest
class TestDemo:
@pytest.fixture()
def login(self):
print('登录')
token = '376411112'
return token
@pytest.fixture()
def connectDB(self):
print('连接数据库')
def test_search(self):
print('搜索')
def test_cart(self):
print('购物车')
def test_order(self, login, connectDB):
print(login)
print('先登录再下单')

测试用例也可以用装饰器 @pytest.mark.usefixtures() 来引用
import pytest
class TestDemo2:
@pytest.fixture()
def login(self):
print('登录')
token = '376411112'
return token
@pytest.fixture()
def connectDB(self):
print('连接数据库')
def test_search(self):
print('搜索')
def test_cart(self, login, connectDB):
print(login)
print('购物车')
@pytest.mark.usefixtures('login', 'connectDB')
def test_order(self):
# print(login) #加装饰器无法获取login()返回值
print('先登录再下单')

fixture 的作用域
默认作用域为 function
@pytest.fixture(scope='function')
| 取值 | 范围 | 说明 |
|---|---|---|
| function | 函数级 | 每个函数或方法都会调用 |
| class | 类级别 | 每个测试类只运行一次 |
| module | 模块级别 | 每一个.py文件只调用一次 |
| package | 包级 | 每一个python包只调用一次 |
| session | 会话级 | 每次会话只需要运行一次,会话内所有方法及类、模块都共享这个方法 |
yield 关键字区分前置/后置代码
yield 之前相当于 setup, yield 之后相当于 teardown
@pytest.fixture()
def login(self):
print('登录')
token = '376411112'
yield token
print('退出登录')
使用 conftest.py 进行数据共享
如果你与其它测试工程师合作开发时,将 fixture 方法放在 conftest.py 中进行数据共享。
如上面的例子中,将 @pytest.fixture() 写在 conftest.py 中,在执行测试用例时,执行到参数 login 时会先从本文件中查找是否有该名称的 fixture 方法,之后会从 conftest.py 中查找,找到该 fixture 方法后继续执行测试用例。
注意事项:
-
conftest.py 文件名称不能改变
-
conftest.py 与运行的用例要在同一个 package 下,并且需要有 __init__.py 文件
-
无需 import 导入 conftest.py
-
所有同目录下测试文件运行前都会执行 conftest.py
-
全局配置和前期工作都可以写入 conftest.py 中,放在某个包下,就是这个包数据共享的地方

浙公网安备 33010602011771号