0


接口自动化框架之python pytest-mark(三)

一、mark标签介绍

在测试用例/测试类前面加上:

@pytest.mark.标签名,

打标记范围:测试用例、测试类、模块文件

二、使用mark进行分类

在使用mark标签之前要创建pytest.ini配置文件,同样在运行的时候,‘-m’参数后边也要标识分类标签的名称

1.创建测试代码

import pytest

def test_01():
    print('oi')

@pytest.mark.number_01
def test_02():
    print('iu')

def test_03():
    print('iuu')

2.配置文件:(注意:一定是pytest.ini)

[pytest]

markers=
    number_01: 分类1
    number_02: 分类2
    number_03: 分类3

3.执行:

if __name__ == '__main__':
    pytest.main(['-s','-v','-m','number_01','test_mark.py'])

完整代码:

import pytest

def test_01():
    print('oi')

@pytest.mark.number_01
def test_02():
    print('iu')

def test_03():
    print('iuu')

if __name__ == '__main__':
    pytest.main(['-s','-v','-m','number_01','test_mark.py'])

三、mark参数化(基础版,后续会单独再去深入写)

@pytest.mark.parametrize(),括号内写入参数名称和参数值

举例:

import pytest

name_value = ['bob','yumi','xiaoming']

@pytest.mark.parametrize('name',name_value)
def test_01(name):
    print(name)

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

参数化:

use_info =[('tom','123'),('bob','2345'),('cindy','7823')]
@pytest.mark.parametrize('username,pwd',use_info)
def test_login(username,pwd):
    print(username +pwd)

四、设置执行测试用例顺序

1.安装pytest -ordering

pip install pytest-ordering
2.参数order后边数字越大,顺序越靠前(默认是按照书写的顺序来执行)

import pytest

@pytest.mark.run(order=3)
def test_01():
    print('01')

@pytest.mark.run(order=2)
def test_02():
    print('02')

@pytest.mark.run(order=1)
def test_03():
    print('03')

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

五、使用mark来标记用例跳过

1.跳过

1.1无判断直接跳过:@pytest.mark.skip(reason ='跳过的原因')

1.2根据条件判断跳过:@pytest.mark.skipif(version<8,reason='版本过低')

import pytest

@pytest.mark.skip(reason ='跳过的原因')
def test_01():
    print('01')

version = 4.0
@pytest.mark.skipif(version<8,reason='版本过低')
def test_02():
    print('02')

def test_03():
    print('03')

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

六、使用mark标记超时时间

6.1安装pytest-timeout插件,在我们做接口自动化的时候,很经常会有一个指标是找出一些耗时的接口,从而告知开发这些接口需要优化

pip install pytest-timeout

tips:可能是由于pytest版本过高,导致目前只能安装timeouts插件, pip install pytest-timeout 更常用,但pip install pytest-timeouts 更强大

6.2基于函数的超时设置

from time import sleep
import pytest

@pytest.mark.timeout(3) #设置超时时间为5s
def test_01():
    sleep(5)
    print('01')

def test_02():
    print('02')

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

本文转载自: https://blog.csdn.net/Immoralay/article/details/124845279
版权归原作者 Immoralay 所有, 如有侵权,请联系我们删除。

“接口自动化框架之python pytest-mark(三)”的评论:

还没有评论