pytest框架_conftest.py详解

再说:
在这里插入图片描述
如图,当你执行测试用例文件test_1.py文件时候,pytest框架执行顺序:
(1)先识别到suit上面的conftest文件,并执行它
(2)再识别到test_1上面的conftest文件,并执行它
(3)执行test_1.py文件

conftest.py简介:
(1)常用来处理测试用例的前置条件。并用fixture标记一些函数,供测试用例的文件test_1.py调用。
(2)文件名称是pytest框架写死的,suit上的conf文件里的内容test_1、2都会执行,test_1上的conf文件只有执行test_1才会执行。
(3)test_1.py文件里面不需要import这个文件,pytest自动执行它。
(4)文件里面的内容

suit上的conftest.py文件

# coding=utf-8
import pytest
from selenium import webdriver


@pytest.fixture(autouse=True)
def project_session_start():
    print('\n启动浏览器')
    yield
    print('\n退出浏览器')

test_1上的conf文件:

# coding=utf-8
import pytest
from selenium import webdriver
import logging

logger = logging.getLogger()

@pytest.fixture(autouse=True)
def start_module():
    print('---进入要执行模块的的界面---')

test_1,py代码,及执行后情况:

# coding=utf-8
import pytest

@pytest.fixture()
def test_case_3():
    print('---3号用例完成---')

@pytest.fixture()
def test_case_4():
    print('---4号用例完成---')

@pytest.fixture()
def test_case_5():
    print('---5号用例完成---')

@pytest.fixture()
def test_case_6():
    print('---6号用例完成---')

@pytest.fixture()
def test_case_7():
    print('---7号用例完成---')

# (1)这里按照【从下到上的顺序】,执行优先级是3、4、5
@pytest.mark.usefixtures('test_case_5')
@pytest.mark.usefixtures('test_case_4')
@pytest.mark.usefixtures('test_case_3')
class Testlogin001:

    def test_case_1(self):
        print('---1号用例完成---')

    # (2)这里按照调用了前面的函数test_case_6,局部的调用,执行优先级是最高的。
    @pytest.mark.usefixtures('test_case_7')
    @pytest.mark.usefixtures('test_case_6')
    def test_case_2(self):
        print('---2号用例完成---')


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

C:\Python39\python.exe D:/se_frame/Cases/MapAaaCases/test_1.py
collecting ... collected 2 items

test_1.py::Testlogin001::test_case_1 
启动浏览器
---进入要执行模块的的界面---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---1号用例完成---
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_2 
启动浏览器
---进入要执行模块的的界面---
---6号用例完成---
---7号用例完成---
---3号用例完成---
---4号用例完成---
---5号用例完成---
---2号用例完成---
PASSED
退出浏览器

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿_焦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值