本文主要是介绍pytest conftest通过fixture实现变量共享,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
conftest.py
scope="module" 只对当前执行的python文件 作用
@pytest.fixture(scope="module")
def global_variable():my_dict = {}yield my_dict
test_case7.py
import pytestlist1 = []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 pytestdef 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博客
这篇关于pytest conftest通过fixture实现变量共享的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!