pytest pytest.ini配置 用例分组 用例跳过
一.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('发短信')
执行
不需要什么参数直接执行就行