pytest 常见用法
2024-08-28 08:53:01
前言
之前一篇文章简单介绍了 pytest 以及 pytest.fixture 装饰器 :https://www.cnblogs.com/shenh/p/11572657.html 。实际在写自动化测试脚本中,还会有一些很实用的方法,下文就来讲述下这些用法。
一.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 =======================
最新文章
- 张高兴的 UWP 开发笔记:横向 ListView
- 将mvc2升级到mvc4
- 配置rsync服务,数据同步。
- EF CodeFirst 使用T4模板 生成文件
- bzoj2002弹(dan)飞绵羊 分块水过
- Struts2.3.15.1源码浅析
- oracle的面试问题
- 数往知来 JavaScript<;十三>;
- C语言 结构体的内存对齐问题与位域
- Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析
- python爬虫抓站的一些技巧总结
- Chapter 2 Open Book——22
- Python3 标准库概览
- DSAPI 简单WebAPI实现
- 【C/C++】小坑们
- R语言curve绘图函数
- Kafka特性
- Spring配置Quartz任务调度、及 ThreadPool 线程池
- Linux下禅道系统的搭建
- 解决python2.7 UnicodeDecodeError和UnicodeEncodeError问题