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

相关推荐
小小测试开发16 小时前
Agent 安全测试:pytest 原生红队框架 RAMPART 实战解析
人工智能·pytest
不拘一格的叶三少5 天前
Pytest + Playwright 自动化测试工程设计要点 & 目录结构
测试工具·pytest
努力搬砖的咸鱼6 天前
意图理解:让Agent从需求描述自动生成Pytest测试策略
人工智能·python·ai·单元测试·pytest·agent
St_rive9 天前
Pytest skip&&skipif跳过⽤例
运维·服务器·pytest
努力搬砖的咸鱼12 天前
AI Agent测试全景图:它到底改变了什么
人工智能·python·ai·集成测试·pytest·agent·ai编程
X1A0RAN13 天前
自动化流水线提效·微观篇:pytest 执行顺序优化
自动化·pytest
久久学姐21 天前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm
小白上线*^_^*21 天前
Pytest 自动化测试框架速通指南(二)
软件测试·pytest·python测试框架·ai测试基础
Logintern0922 天前
pytest的AST 断言重写
pytest