pytest 常用方法介绍
前言
之前一篇文章简单介绍了 pytest 以及 pytest.fixture 装饰器 :Pytest 使用简介 - 三只松鼠 - 博客园 。实际在写自动化测试脚本中,还会有一些很实用的方法,下文就来讲述下这些用法。
一.pytest.mark.parametrize 装饰器
pytest 内置装饰器 @pytest.mark.parametrize 可以让测试数据参数化,把测试数据单独管理,类似 ddt 数据驱动的作用,方便代码和测试数据分离。
1.一次传多个参数
import pytest
@pytest.mark.parametrize('x,y',[(1,2),(3,4)])
def test_sum(x,y):
sum = x + y
print(sum)
if __name__ =="__main__":
pytest.main(['test_sample.py','-s'])
执行结果:
test_sample.py
3
.
7
.
============================== 2 passed in 0.06s ==============================
2.组合传参:
注意:这种方式一共传递了4组参数 (1,3)、(1,4)、(2,3)、(2,4)。这种方式可以简化测试数据,不用手动再将参数组合。
import pytest
@pytest.mark.parametrize('x',[1,2])
@pytest.mark.parametrize('y',[3,4])
def test_sum(x,y):
sum = x + y
print(sum)
if __name__ =="__main__":
pytest.main(['test_sample.py','-s'])
执行结果:
test_sample.py
4
.
5
.
5
.
6
.
============================== 4 passed in 0.14s ==============================
二、fixture返回值
1.获取被调用函数返回值
import pytest
@pytest.fixture(scope='function')
def login():
accesstoken = '197ce8083c38467f'
return accesstoken
def test_sum(login):
token = login
print(token)
if __name__ =="__main__":
pytest.main(['test_sample.py','-s'])
执行结果:
test_sample.py
197ce8083c38467f
.
============================== 1 passed in 0.04s ==============================
若被调用函数返回多个参数:
import pytest
@pytest.fixture(scope='function')
def login():
accesstoken = '197ce8083c38467f'
customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'
return accesstoken,customerguid
def test_sum(login):
token = login[0]
guid = login[1]
print(token)
print(guid)
if __name__ =="__main__":
pytest.main(['test_sample.py','-s'])
执行结果:
test_sample.py
197ce8083c38467f
096799f5-e040-11e9-8c01-0242ac11000d
.
============================== 1 passed in 0.07s ==============================
2.单个用例调用多个函数
import pytest
@pytest.fixture(scope='function')
def login():
print('登录')
@pytest.fixture(scope='function')
def conn():
print('连接数据库')
def test_1(login,conn):
print('测试用例1')
def test_2():
print('测试用例2')
if __name__ =="__main__":
pytest.main(['test_sample.py','-s'])
执行结果:
test_sample.py
登录
连接数据库
测试用例1
.
测试用例2
.
============================== 2 passed in 0.05s ==============================
三、测试用例分类
有时候我们只需执行部分测试用例,比如从用例集当中挑选 smoke 测试,要怎么做呢?通过装饰器 @pytest.mark.smoke,smoke 是可以自定义的,运行时加上命令‘-m=smoke’,pytest 就会挑选带有装饰器的类或函数运行。
import pytest
@pytest.fixture(scope='function')
def login():
accesstoken = '197ce8083c38467f'
customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'
return accesstoken,customerguid
@pytest.mark.smoke
def test_sum(login):
token = login[0]
guid = login[1]
print(token)
print(guid)
def test_2():
print('测试用例')
if __name__ =="__main__":
pytest.main(['test_sample.py','-s','-m=smoke'])
执行结果:
test_sample.py
197ce8083c38467f
096799f5-e040-11e9-8c01-0242ac11000d
.
======================= 1 passed, 1 deselected in 0.02s =======================
四、skipif-跳过测试
1.pytest.skip (用于函数内,跳过测试用例)
def test_2():
if 1 < 2:
pytest.skip('1111111')
pass
1.1也可以用于模块级别,跳过当前模块里所有的测试用例。(注:参数allow_module_level的值设为True)
if 1==1:
pytest.skip('1111111',allow_model_level=True)
def test_1():
pass
def test_2():
pass
2.@pytest.mark.skip(用于函数外,跳过测试用例)
@pytest.mark.skip(reason='feature not implemented')
def test_1():
pass
# 模块级别跳过。(注:参数allow_module_level的值设为True)
pytest.skip('skip all tests', allow_module_level=True)
3.@pytest.mark.skipif(用于函数外,条件condition,跳过原因reason="xxx")
@pytest.mark.skipif(condition='1<2',reason='feature not implemented')
def test_1():
pass
五、控制执行顺序
order-执行顺序
1、控制用例执行顺序的方法
2、在需要调整用例执行顺序的函数(或方法)前增加,如@pytest.mark.run(order=x),x表示数字
3、执行顺序,由小到大、由正到负、未标记的在正数后、负数前执行
顺序为:1,2,3,无标记,-3,-2,-1
4、提示:需要下载 pytest_ordering 依赖包,否则@pytest.mark.run(order=x)既不报错也不会生效
class Testpytest(object):
@pytest.mark.run(order=-1)
def test_two(self):
print("test_two, 测试用例")
@pytest.mark.run(order=3)
def test_one(self):
print("test_one, 测试用例")
@pytest.mark.run(order=1)
def test_three(self):
print("test_three, 测试用例")
六、预期失败
1.xfail-预期失败
xfail-预期失败的函数语法xfail(condition, reason):
--condition 预期失败的条件
--reason 预期失败的原因
# condition 条件相等判断失败
@pytest.mark.xfail(condition='1==1', reason="The test case")
def test_1():
print("\n-------")
# condition 条件不等判断成功
@pytest.mark.xfail(condition='1==2', reason="The test case")
def test_2():
print("\n-------")
版权声明:本文为weixin_65784341原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。