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博客

相关推荐
我的xiaodoujiao11 小时前
API 接口自动化测试详细图文教程学习系列11--Requests模块3--测试练习
开发语言·python·学习·测试工具·pytest
我的xiaodoujiao1 天前
API 接口自动化测试详细图文教程学习系列12--Requests模块4--测试实践操作
python·学习·测试工具·pytest
Lightning-py2 天前
pytest 软断言的几种方式
pytest
Lightning-py3 天前
pytest后置处理方式
pytest
爆更小哇4 天前
Python自动化测试:pytest新手快速入门指南
python·测试工具·自动化·pytest
qq_452396234 天前
第一篇:基座选型 —— 为什么 Pytest 是自动化的终点?
自动化·pytest
时光不写代码5 天前
修复 pytest-asyncio 事件循环冲突:完整解决方案
python·pytest·fastapi
上天_去_做颗惺星 EVE_BLUE5 天前
接口自动化测试全流程:pytest 用例收集、并行执行、Allure 报告合并与上传
python·pytest
踏着七彩祥云的小丑5 天前
pytest——Mark标记
开发语言·python·pytest
lifewange6 天前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest