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的更多相关文章

  1. pytest参数化 parametrize

    pytest.mark.parametrize装饰器可以实现测试用例参数化 parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of test ...

  2. Pytest 系列(28)- 参数化 parametrize + @allure.title() 动态生成标题

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 参数化 @pytest.ma ...

  3. 【pytest】使用parametrize将参数化变量传递到fixture

    分享一个关于在pytest中,如何将测试用例文件中的变量传递到fixture函数. 一.交代应用场景 目前组内的项目,在根目录下是有一个conftest.py文件的,这里有个生成api token的f ...

  4. 预期结果 参数化parametrize

    1.pytest.mark.parametrize装饰器可以实现测试用例参数化. 2.实例: import pytest @pytest.mark.parametrize("req,expe ...

  5. pytest的参数化

    参数化有两种方式: 1. @pytest.mark.parametrize 2.利用conftest.py里的 pytest_generate_tests 1中的例子如下: @pytest.mark. ...

  6. pytest_参数化parametrize

    前言 pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 import pytest ...

  7. pytest(8)-参数化

    前言 什么是参数化,通俗点理解就是,定义一个测试类或测试函数,可以传入不同测试用例对应的参数,从而执行多个测试用例. 例如对登录接口进行测试,假设有3条用例:正确账号正确密码登录.正确账号错误密码登录 ...

  8. pytest.5.参数化的Fixture

    From: http://www.testclass.net/pytest/parametrize_fixture/ 背景 继续上一节的测试需求,在上一节里,任何1条测试数据导致断言不通过后测试用例就 ...

  9. pytest的参数化测试

    感觉在单元测试当中可能有用, 但在django这种框架中,用途另一说. import pytest import tasks from tasks import Task def test_add_1 ...

随机推荐

  1. Canvas & SVG

    Canvas & SVG https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-dev ...

  2. 莫烦scikit-learn学习自修第二天【算法地图】

    1. 算法地图

  3. PHP爬虫框架Snoopy的使用

    参考文档: http://ibillxia.github.io/blog/2010/08/10/php-connecting-tool-snoopy-introduction-and-applicat ...

  4. sql 用户相关命令

    查看所有用户 select distinct concat(user, '@', host,';') as userList from mysql.user; select  #查找 distinct ...

  5. epoch、 iteration和batchsize区别

    转自: https://blog.csdn.net/qq_27923041/article/details/74927398 深度学习中经常看到epoch. iteration和batchsize,下 ...

  6. 洛谷 p1219 八皇后

    刚参加完蓝桥杯 弱鸡错了好几道..回头一看确实不难 写起来还是挺慢的 于是开始了刷题的道路 蓝桥杯又名搜索杯 暴力杯...于是先从dfs刷起 八皇后是很经典的dfs问题 洛谷的这道题是这样的 上面的布 ...

  7. Qt evenFilter()与installEvenFilter()

    1, eventFilter 函数中实现事件过滤器.请注意:该函数在 QObject 类中声明为一个虚函数,因此只能由 QObject 的子类继承使用. 2, installEventFilter函数 ...

  8. 删除本地git的远程分支和远程删除git服务器的分支【转】

    转- 删除本地git的远程分支和远程删除git服务器的分支 在项目中使用git管理代码后,有些时候会创建很多不同名称的分支,以此区分各个分支代码功能. 而随着代码的合并,以前的分支就可能不再需要保存了 ...

  9. 在GitHub上读大学:涵盖清华,北大,浙大等大学课程

    来自多位GitHub网友在GitHub分享的几组学习课程项目, 学习课程包含清华,北大,浙大,中科大,上海交大, 等中国多所名校的英语,AI高数,人工智能等课程以及一些讲义考题. 如果你想了解这些大学 ...

  10. Codeforces Round #432 Div. 1

    A:大胆猜想合法点不会很多,于是暴力检验,一旦发现不合法就break,可以random_shuffle一下. #include<iostream> #include<cstdio&g ...