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

相关推荐
唐古乌梁海12 小时前
【pytest】编写自动化测试用例命名规范README
自动化·pytest
xing251612 小时前
pytest下allure
开发语言·python·pytest
IT求学人2 天前
pytest运行用例的常见方式及参数
pytest
开源优测3 天前
什么是pytest.ini及如何在Pytest中应用以提升配置效率
pytest
摸鱼仙人~3 天前
ImportError: cannot import name ‘FixtureDef‘ from ‘pytest‘
conda·pytest·fastapi
唐古乌梁海4 天前
【pytest-jira】自动化用例结合jira初版集成思路
pytest·jira
小码哥说测试5 天前
高效执行自动化用例:分布式执行工具pytest-xdist实战!
自动化测试·软件测试·功能测试·jmeter·pytest·postman·测试工程师
予早5 天前
pytest asyncio 支持插件 pytest-asyncio
pytest