pytest的conftest使用

项目test下的目录结构

 

全局conftest.py

import pytest
# 当autouse=False时,测试用例需要传入参数,为True时,不需要传入参数
@pytest.fixture(scope="session",autouse=True)
def start():
    print('\n打开首页')
    # flag = 1
    # yield flag
    # print('退出登录')

suite1/conftest.py

import pytest
# 当autouse=False时,测试用例需要传入参数,为True时,不需要传入参数
@pytest.fixture(scope="module")
def open_web1():
    print('\n打开页面1')

suite1/test_1.py

import pytest
def test_03(start,open_web1):
    print('测试用例3操作')
def test_04(start,open_web1):
    print('测试用例4操作')

if __name__ == "__main__":
    pytest.main(["-s", "test_1.py"])

suite1/test_2.py

import pytest
def test_05(start,open_web1):
    print('测试用例5操作')
def test_06(start,open_web1):
    print('测试用例6操作')

if __name__ == "__main__":
    pytest.main(["-s", "test_2.py"])

suite2/conftest.py

import pytest
# 当autouse=False时,测试用例需要传入参数,为True时,不需要传入参数
@pytest.fixture(scope="function")
def open_web2():
    print('\n打开页面2')

suite2/test_3.py

import pytest
def test_07(start,open_web2):
    print('测试用例7操作')
def test_08(start,open_web2):
    print('测试用例8操作')

if __name__ == '__main__':
    pytest.main(["-s", "test_3.py"])

suite2/test_4.py

import pytest
def test_09(start,open_web2):
    print('测试用例9操作')
def test_10(start,open_web2):
    print('测试用例10操作')
if __name__ == '__main__':
    pytest.main(["-s", "test_4.py"])

执行测试

 

 

 

posted @   爱语默  阅读(3785)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示