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

相关推荐
程序员杰哥23 分钟前
Pytest与Unittest测试框架对比
自动化测试·软件测试·python·测试工具·测试用例·excel·pytest
软件测试小仙女24 分钟前
Pytest参数化实战:高效测试API接口
软件测试·测试开发·测试工具·pytest·接口测试·api·参数化
子正25 分钟前
Pytest单元测试一例:u16采样值格式转换的错误
单元测试·pytest
cllsse16 小时前
pytest学习
软件测试·python·pytest
Test.X2 天前
学习16天:pytest学习
学习·pytest
唐古乌梁海3 天前
【pytest 】 pytest 生命周期
pytest
专职4 天前
pytest详细教程
开发语言·python·pytest
乄捷径4 天前
pytest入门到熟练
pytest
专职4 天前
pytest+requests+allure生成接口自动化测试报告
开发语言·python·pytest
小熊出擊5 天前
【pytest】fixture 内省(Introspection)测试上下文
python·单元测试·pytest