pytest pytest.ini配置 用例分组 用例跳过

本文详细介绍了如何使用pytest配置文件pytest.ini进行用例分组,包括冒烟测试和回归测试,并展示了如何通过markers实现用例的无条件和有条件跳过。通过指定命令行参数,可以方便地执行特定分组的测试用例,提高测试效率。
摘要由CSDN通过智能技术生成

一.pytest.ini配置

1.文件位置,根目录下

addopts = -vs 配置默认携带的参数
testpaths = ./basics 配置默认执行用例的目录
配置用例分组的便签名
markers =
smoke:冒烟用例
huigui:回归用例

2.文件格式编码

使用工具变成ANSI编码,不然无法读取配置文件
在这里插入图片描述

在这里插入图片描述

二.用例分组

代码

@pytest.mark.run(order=1)
这条只属于冒烟用例
@pytest.mark.smoke
def test1():
    print('登录')
@pytest.mark.run(order=4)
def test4():
    print('取件')
@pytest.mark.skip(reason="说明跳过原因")
def test_skip():
    print("测试跳过这条用例")
这条被设置为2个分组,属于冒烟和回归
@pytest.mark.run(order=2)
@pytest.mark.huigui
@pytest.mark.smoke
def test2():
    print('存件')
age=17
@pytest.mark.run(order=3)
这条只属于冒烟用例
@pytest.mark.huigui
@pytest.mark.skipif(age<18,reason="未成年人不需要发短信")
def test3():
    print('发短信')

执行

1.只执行冒烟

pytest.main([r'D:\python新代码集\pytest_study\basics\test_execution_sequence.py','-m smoke'])

在这里插入图片描述

2.只执行回归

pytest.main([r'D:\python新代码集\pytest_study\basics\test_execution_sequence.py','-m huigui'])

在这里插入图片描述

3.执行冒烟和回归的交集

    pytest.main([r'D:\python新代码集\pytest_study\basics\test_execution_sequence.py','-m huigui and smoke'])

在这里插入图片描述

4.执行冒烟和回归的并集

    pytest.main([r'D:\python新代码集\pytest_study\basics\test_execution_sequence.py','-m huigui or smoke'])

在这里插入图片描述

三.用例跳过

知识点

无条件跳过加上跳过原因

@pytest.mark.skip(reason="说明跳过原因")
def test_skip():
    print("测试跳过这条用例")

有条件跳过加上判断条件,满足条件就跳过

age=17
@pytest.mark.run(order=3)
@pytest.mark.huigui
@pytest.mark.skipif(age<18,reason="未成年人不需要发短信")
def test3():
    print('发短信')

代码

@pytest.mark.run(order=1)
@pytest.mark.smoke
def test1():
    print('登录')
@pytest.mark.run(order=4)
def test4():
    print('取件')
@pytest.mark.skip(reason="说明跳过原因")
def test_skip():
    print("测试跳过这条用例")

@pytest.mark.run(order=2)
@pytest.mark.huigui
@pytest.mark.smoke
def test2():
    print('存件')

age=17
@pytest.mark.run(order=3)
@pytest.mark.huigui
@pytest.mark.skipif(age<18,reason="未成年人不需要发短信")
def test3():
    print('发短信')

执行

不需要什么参数直接执行就行
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

亚索不会吹风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值