pytest 8 参数化parametrize
pytest.mark.parametrize装饰器可以实现用例参数化
1.以下是一个实现检查一定的输入和期望输出测试功能的典型例子
import pytest @pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42)])
def test_add(test_input,expected):
assert eval(test_input) == expected 结果:成功2个失败1个,但是需要注意的是,@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42)])里面的"test_input,expected"一定要和
test_add(test_input,expected)当中的参数名称一致,否则,将会出错。
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 3 items
parametrizing.py ..F
parametrizing.py:9 (test_add[6*9-42])
42 != 54
Expected :54
Actual :42
<Click to see difference>
test_input = '6*9', expected = 42
@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42)])
def test_add(test_input,expected):
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
parametrizing.py:12: AssertionError
[100%]
=================================== FAILURES ===================================
_______________________________ test_add[6*9-42] _______________________________
test_input = '6*9', expected = 42
@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42)])
def test_add(test_input,expected):
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9')
parametrizing.py:12: AssertionError
================ 1 failed, 2 passed, 1 warnings in 0.08 seconds ================
Process finished with exit code 0
2.它也可以标记单个测试实例在参数化,例如使用内置的mark.xfail
import pytest @pytest.mark.parametrize("test_input,expected", [
("3+5", 8),
("2+4", 6),
pytest.param("6 * 9",42,marks=pytest.mark.xfail),])
def test_eval(test_input, expected):
print("-------开始用例------")
assert eval(test_input) == expected 结果:可以看出来,标记为失败的用例就不运行了,直接跳过显示xfailed
============================= test session starts ==============================
platform darwin -- Python 2.7.10, pytest-3.6.3, py-1.5.2, pluggy-0.6.0
rootdir: /Users/newcomer/PycharmProjects/error/wuya/pytestDemo, inifile:
plugins: metadata-1.7.0, html-1.19.0, D3-2.0.13, cov-2.5.1, catchlog-1.2.2, allure-adaptor-1.7.10, georaven-17.1.0.170collected 3 items
parametrizing.py .-------开始用例------
.-------开始用例------
x-------开始用例------
test_input = '6 * 9', expected = 42
@pytest.mark.parametrize("test_input, expected", [
("3+5", 8),
("2+4", 6),
pytest.param("6 * 9",42,marks=pytest.mark.xfail),])
def test_eval(test_input, expected):
print("-------开始用例------")
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6 * 9')
parametrizing.py:26: AssertionError
[100%]
=============== 2 passed, 1 xfailed in 0.08 seconds ================
Process finished with exit code 0
参数组合:
import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
def test_foo(x, y):
print("测试数据组合:x->%s, y->%s" % (x, y)) 结果:
test_canshu1.py 测试数据组合:x->0, y->2
.测试数据组合:x->1, y->2
.测试数据组合:x->0, y->3
.测试数据组合:x->1, y->3 .
========================== 4 passed in 1.75 seconds ===========================
这将运行测试,参数设置为x=0/y=2,x=1/y=2,x=0/y=3,x=1/y=3组合参数。
pytest 8 参数化parametrize的更多相关文章
- 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】使用parametrize将参数化变量传递到fixture
分享一个关于在pytest中,如何将测试用例文件中的变量传递到fixture函数. 一.交代应用场景 目前组内的项目,在根目录下是有一个conftest.py文件的,这里有个生成api token的f ...
- 预期结果 参数化parametrize
1.pytest.mark.parametrize装饰器可以实现测试用例参数化. 2.实例: import pytest @pytest.mark.parametrize("req,expe ...
- pytest的参数化
参数化有两种方式: 1. @pytest.mark.parametrize 2.利用conftest.py里的 pytest_generate_tests 1中的例子如下: @pytest.mark. ...
- pytest_参数化parametrize
前言 pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 import pytest ...
- pytest(8)-参数化
前言 什么是参数化,通俗点理解就是,定义一个测试类或测试函数,可以传入不同测试用例对应的参数,从而执行多个测试用例. 例如对登录接口进行测试,假设有3条用例:正确账号正确密码登录.正确账号错误密码登录 ...
- pytest.5.参数化的Fixture
From: http://www.testclass.net/pytest/parametrize_fixture/ 背景 继续上一节的测试需求,在上一节里,任何1条测试数据导致断言不通过后测试用例就 ...
- pytest的参数化测试
感觉在单元测试当中可能有用, 但在django这种框架中,用途另一说. import pytest import tasks from tasks import Task def test_add_1 ...
随机推荐
- 运维常用mysql语句
1..select @@version; ##查询当前mysql的版本. 2. show variables like 'port';##查看mysql实例的端口. 3.show variables ...
- 利用Python制作简单的小程序:IP查看器
前言 说实话,查看电脑的IP,也挺无聊的,但是够简单,所以就从这里开始吧.IP地址在操作系统里就可以直接查看.但是除了IP地址,我们也想通过IP获取地理地址和网络运营商情况.IP地址和地理地址并没有固 ...
- 集合之TreeMap(含JDK1.8源码分析)
一.前言 前面所说的hashMap和linkedHashMap都不具备统计的功能,或者说它们的统计性能的时间复杂度都不是很好,要想对两者进行统计,需要遍历所有的entry,时间复杂度比较高,此时,我们 ...
- nginx rewrite重写
通过官方文档可以看到,rewrite的作用上下文是 server location,可以写在 server里面 亦或location里面; 命令: if (条件) {} 条件判断 set #设置 ...
- SQL 添加索引
使用CREATE 语句创建索引 CREATE INDEX index_name ON table_name(column_name,column_name) include(score) 普通索引 C ...
- hdu-5687(字典树)
题意:中文题: 解题思路:增加和查询就不说了,标准操作,就是删除操作:删除操作的时候,我们把给定字符串先在字典树中遍历一遍,然后算出这个字符串最后一个字符的出现次数,然后在遍历一遍,每个节点都减去这个 ...
- 洛谷-p2764(最小路径覆盖)(网络流24题)
#include<iostream> #include<algorithm> #include<queue> #include<cstring> #in ...
- Nginx lingering_close延迟关闭
L:130
- Nginx 反向代理接收用户包体方式
陶辉91课 如果proxy_request_buffering 设置为on的时候是等待nginx读取完包体后再发送上游服务器 一般依赖于nginx处理能力 client_body_in_file_o ...
- 洛谷 P2151 [SDOI2009]HH去散步
题目链接 思路 如果没有不能走上一条边的限制,很显然就是dp. 设f[i][j]表示到达i点走了j步的方案数,移到k点可以表示为f[k][j+1]+=f[i][j]. 如果有限制的话,可以考虑用边表示 ...