pytest_参数化parametrize
前言
pytest.mark.parametrize装饰器可以实现测试用例参数化。
parametrizing
1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子
import pytest @pytest.mark.parametrize(
"test_input, expected",
[
("3+5", 8),
("5+7", 12),
("9/3", 3),
("6*9", 42)
]
)
def test_eval(test_input, expected):
print("______测试用例_______")
assert eval(test_input) == expected if __name__ == '__main__':
pytest.main(["-s", "Parametrize_01.py"])
运行结果
============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: D:\python_auto\s14\pytest_learn, inifile:
collected 4 items Parametrize_01.py ______测试用例_______
.______测试用例_______
.______测试用例_______
.______测试用例_______
F ================================== FAILURES ===================================
______________________________ test_eval[6*9-42] ______________________________ test_input = '6*9', expected = 42 @pytest.mark.parametrize(
"test_input, expected",
[
("3+5", 8),
("5+7", 12),
("9/3", 3),
("6*9", 42)
]
)
def test_eval(test_input, expected):
print("______测试用例_______")
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9') Parametrize_01.py:18: AssertionError
===================== 1 failed, 3 passed in 0.07 seconds ======================
在这个例子中设计的,只有一条输入/输出值的简单测试功能。和往常一样
函数的参数,你可以在运行结果看到在输入和输出值
2.它也可以标记单个测试实例在参数化,例如使用内置的mark.xfail
import pytest @pytest.mark.parametrize(
"test_input, expected",
[
("3+5", 8),
("5+7", 12),
("9/3", 3),
pytest.param("6*9", 42,
marks=pytest.mark.xfail),
]
)
def test_eval(test_input, expected):
print("______测试用例_______")
assert eval(test_input) == expected if __name__ == '__main__':
pytest.main(["-s", "Parametrize_02.py"])
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: D:\python_auto\s14\pytest_learn, inifile:
collected 4 items Parametrize_02.py ______测试用例_______
.______测试用例_______
.______测试用例_______
.______测试用例_______
x ===================== 3 passed, 1 xfailed in 0.10 seconds =====================
标记为失败的用例就不运行了,直接跳过显示xfailed
参数组合
1.若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器
import pytest
@pytest.mark.parametrize("x", [0,1])
@pytest.mark.parametrize("y", [2,3])
def test_eval(x, y):
print("______测试用例_______")
print("测试数据组合: x -> %s, y -> %s"% (x, y))
if __name__ == '__main__':
pytest.main(["-s", "Parametrize_03.py"])
运行结果
============================= test session starts =============================
platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1
rootdir: D:\python_auto\s14\pytest_learn, inifile:
collected 4 items Parametrize_03.py ______测试用例_______
测试数据组合: x -> 0, y -> 2
.______测试用例_______
测试数据组合: x -> 1, y -> 2
.______测试用例_______
测试数据组合: x -> 0, y -> 3
.______测试用例_______
测试数据组合: x -> 1, y -> 3
. ========================== 4 passed in 0.05 seconds ===========================
这将运行测试,参数设置为x=0/y=2,x=1/y=2,x=0/y=3,x=1/y=3组合参数。
作者:含笑半步颠√
博客链接:https://www.cnblogs.com/lixy-88428977
声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。
pytest_参数化parametrize的更多相关文章
- pytest 8 参数化parametrize
pytest.mark.parametrize装饰器可以实现用例参数化 1.以下是一个实现检查一定的输入和期望输出测试功能的典型例子 import pytest @pytest.mark.parame ...
- 预期结果 参数化parametrize
1.pytest.mark.parametrize装饰器可以实现测试用例参数化. 2.实例: import pytest @pytest.mark.parametrize("req,expe ...
- pytest_参数化3
import pytesttest_user_data=[ {'user':'linda','password':'8888'}, {'user':'servenruby','password':'1 ...
- pytest参数化 parametrize
pytest.mark.parametrize装饰器可以实现测试用例参数化 parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of test ...
- Pytest 系列(28)- 参数化 parametrize + @allure.title() 动态生成标题
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 参数化 @pytest.ma ...
- Pytest_参数化(10)
pytest参数化有两种方式: mark的parametrize标记:@pytest.mark.parametrize(变量名,变量值),其中变量值类型为列表.元组或其它可迭代对象. fixture的 ...
- pytest_参数化之3*3
import pytesttest_user_data1=[{'user':'linda','password':'888888'}, {'user':'servenruby','password': ...
- pytest文档8-参数化(parametrize)结合allure.title()生成不同标题报告
参数化parametrize 先看一个简单的pytest参数化案例演示test_a.py # test_a.py import pytest import allure def login(usern ...
- 比培训机构还详细的 Python 学习路线,你信吗 0^0
前言 这其实是将自己写的文章进行一个总结分类,并不代表最佳学习路线 会不断更新这篇文章...没链接的文章正在编写ing...会不会哪天我的这个目录就出现在培训机构的目录上了... 目前实战比较少(要是 ...
随机推荐
- java stackoverflowerror与outofmemoryerror区别
1.stackoverflow: 每当java程序启动一个新的线程时,java虚拟机会为他分配一个栈,java栈以帧为单位保持线程运行状态:当线程调用一个方法是,jvm压入一个新的栈帧到这个线程的栈中 ...
- 轮播模仿臭美APP,vue,swiper
介绍:轮播使用了swiper,重要用于移动端滑动,详情可查看官网 1.首先用npm安装 npm install swiper 2.main.js 中引入CSS import 's ...
- 从工厂流水线小妹到Google上班程序媛,看完后,我跪服了!
阅读本文大概需要 10.2 分钟. 文作者:Ling Sun 原文链接:https://www.zhihu.com/question/68154951/answer/546265013 我家境很不好, ...
- Linux kill、kill-15、kill-9区别
进程状态转换图 kill和kill -9,两个命令在linux中都有杀死进程的效果,然而两命令的执行过程却大有不同,在程序中如果用错了,可能会造成莫名其妙的现象. 执行kill(不加 -* 默认kil ...
- List和数组的互转
list转数组: /要转换的list集合 List<String> testList = new ArrayList<String>(){{add("aa" ...
- Schema注册表客户端
Schema注册表客户端 与模式注册表服务器交互的客户端抽象是SchemaRegistryClient接口,具有以下结构: public interface SchemaRegistryClient ...
- 一行命令搞定/usr/bin/perl^M: bad interpreter
https://www.cnblogs.com/albertYe/p/8819143.html **************************************************** ...
- LeetCode_459. Repeated Substring Pattern
459. Repeated Substring Pattern Easy Given a non-empty string check if it can be constructed by taki ...
- [Golang] go modules使用
关于go modules的使用外面的教程实在太多了,我这里只讲下我自己使用的三种情形. 准备工作: 1.新建个文件加gomod_test. 2.在这个目录输入命令 go mod init gomod_ ...
- DataPipeline |ApacheKafka实战作者胡夕:Apache Kafka监控与调优
https://baijiahao.baidu.com/s?id=1610644333184173190&wfr=spider&for=pc DataPipeline |ApacheK ...