pytest conftest通过fixture实现变量共享

 pytest conftest通过fixture实现变量共享_第1张图片

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

 pytest conftest通过fixture实现变量共享_第2张图片

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

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

pytest conftest通过fixture实现变量共享_第3张图片

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

你可能感兴趣的:(#,pytest测试框架,pytest)