pytest conftest通过fixture实现变量共享

conftest.py

scope="module" 只对当前执行的python文件 作用

复制代码
@pytest.fixture(scope="module")
def global_variable():
    my_dict = {}
    yield my_dict

test_case7.py

复制代码
import pytest

list1 = []


def test_case001(global_variable):

    data1 = '123'
    global_variable.update({'test_case_data1': data1})


def test_case002(global_variable):
    print('\n', global_variable)
    data2 = '123'
    global_variable.update({'test_case_data2': data2})

def test_case003(global_variable):

    print('\n', global_variable)

if __name__ == '__main__':
    pytest.main(['s', 'v', 'test_case7.py'])
    pass

test_case8.py

复制代码
import pytest


def test_case001(global_variable):

    print('\n', global_variable)

if __name__ == '__main__':
    pytest.main(['s', 'v', 'test_case8.py'])
    pass

scope="session" 可跨py文件共享变量

复制代码
# 在 conftest.py 中定义全局变量
@pytest.fixture(scope="session")
def global_variable():
    my_dict = {}
    yield my_dict

Pytest fixture 的四种作用域:session、module、class 和 function-CSDN博客

相关推荐
词元Max2 天前
2.12 pytest 实战:如何测试 AI 应用
人工智能·pytest
biter down3 天前
11:pytest 框架 assert 验证测试
服务器·windows·pytest
biter down4 天前
14:pytest-order 插件 顺序控制案例
开发语言·python·pytest
我的xiaodoujiao4 天前
API 接口自动化测试详细图文教程学习系列21--结合Pytest框架使用2--断言和插件
python·学习·测试工具·pytest
我的xiaodoujiao4 天前
API 接口自动化测试详细图文教程学习系列22--结合Pytest框架使用3-分组、跳过执行和参数化处理
python·学习·测试工具·pytest
测试员周周8 天前
【Appium 系列】第13节-混合测试执行器 — API + UI 的协同执行
开发语言·人工智能·python·功能测试·ui·appium·pytest
旦莫10 天前
一个完美的AI测试Agent应该是什么样的
人工智能·python·测试开发·pytest·ai测试
我的xiaodoujiao11 天前
API 接口自动化测试详细图文教程学习系列20--结合Pytest框架使用
python·学习·测试工具·pytest