码迷,mamicode.com
首页 > 其他好文 > 详细

pytest 8 参数化parametrize

时间:2018-08-21 18:27:09      阅读:603      评论:0      收藏:0      [点我收藏+]

标签:输出   tde   %s   darwin   war   需要   ada   ems   标记   

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

标签:输出   tde   %s   darwin   war   需要   ada   ems   标记   

原文地址:https://www.cnblogs.com/peiminer/p/9507111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!