From: http://www.testclass.net/pytest/parametrizing_fixture/

背景

@pytest.mark.parametrize 装饰器可以让我们每次参数化fixture的时候传入多个项目。回忆上一节,我们参数化的时候只能传入传入1个字符串或者是其他的数据对象,parametrize每次多个参数,更加灵活。

例子

import pytest
@pytest.mark.parametrize("test_input,expected", [
("3+5", 8),
("2+4", 6),
("6*9", 42),
])
def test_eval(test_input, expected):
assert eval(test_input) == expected

test_eval方法中传入了2个参数,就如同@pytest.mark.parametrize装饰器中定义的那样,因此简单理解,我们可以把parametrize装饰器想象成是数据表格,有表头(test_input,expected)以及具体的数据。

运行结果

$ pytest
======= test session starts ========
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 3 items test_expectation.py ..F ======= FAILURES ========
_______ test_eval[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_eval(test_input, expected):
> assert eval(test_input) == expected
E AssertionError: assert 54 == 42
E + where 54 = eval('6*9') test_expectation.py:8: AssertionError
======= 1 failed, 2 passed in 0.12 seconds ========

pytest.6.Parametrize Fixture的更多相关文章

  1. Pytest系列(9) - 参数化@pytest.mark.parametrize

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest允许在多个级别启 ...

  2. 5.@pytest.mark.parametrize()数据驱动

    简介: pytest.mark.parametrize 是 pytest 的内置装饰器,它允许你在 function 或者 class 上定义多组参数和 fixture 来实现数据驱动. @pytes ...

  3. pytest进阶之fixture

    前言 学pytest就不得不说fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一样,如果不学fixture那么使用pytest和使用unit ...

  4. pytest自动化6:pytest.mark.parametrize装饰器--测试用例参数化

    前言:pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.  下面是一个简单是实例,检查一定的输入和期望输出测试功能的典型例子 2.  标记单 ...

  5. pytest.10.使用fixture参数化测试预期结果

    From: http://www.testclass.net/pytest/test_api_with_expected_result/ 背景 接上一节v2ex网站的查看论坛节点信息的api. 我们在 ...

  6. pytest框架之fixture前置和后置

    一.conftest.py 定义公共的fixture,多个测试类中都可以调用 pytest提供了conftest.py文件,可以将fixture定义在此文件中 运行测试用例时,不需要去导入这个文件,会 ...

  7. Pytest【定制fixture】

    在pytest中的fixture是在测试函数运行前后,由pytest执行的外壳函数,fixture中的代码可以定制,满足多变的测试需求:包括定义传入测试中的数据集.配置测试前系统的初始化状态.为批量测 ...

  8. pytest.mark.parametrize()参数化应用二,读取json文件

    class TestEnorll(): def get_data(self): """ 读取json文件 :return: """ data ...

  9. pytest.mark.parametrize()参数化的应用一

    from page.LoginPage import Loginpage import os, sys, pytest base_dir = os.path.dirname(os.path.dirna ...

随机推荐

  1. [LeetCode&Python] Problem 888. Fair Candy Swap

    Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...

  2. ubuntu上安装boost库

    只需一条命令: sudo apt install libboost-dev

  3. Unity调用Windows对话框保存时另存为弹框

    Unity开发VR之Vuforia 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...

  4. LG4196 [CQOI2006]凸多边形

    题意 题目描述 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. 输入输出格式 输入格式: 第一行有一个整数n,表示凸多边形的个数, ...

  5. python简单实现目录对比

    [root@localhost python]# cat dircmptest.py #!/usr/bin/python import filecmp path1="/root/python ...

  6. codeblock设置快捷键

    第一步: 第二步: 第三步:  格式化代码设置: 在代码框里点右键,按Format use Astyle就会自动代码格式化了 但是它默认的风格是大括号另起一行,很不习惯,实际上是可以改的 1.Sett ...

  7. 02HDFS架构

    https://www.cnblogs.com/zhoujingyu/p/5040957.html https://blog.csdn.net/firstchange/article/details/ ...

  8. java System.arraycopy()

    package study.stage2; import java.util.Arrays; /** * Created by Sandy.Liu on 2017/7/19. */public cla ...

  9. JavaScript跨浏览器绑定事件函数的优化

    JavaScript作为一门基于事件驱动的语言(特别是用在DOM操作的时候),我们常常需要为DOM绑定各种各样的事件.然而,由于低版本的IE的不给力,在绑定事件和移除事件监听上都与众不同,我们常常需要 ...

  10. firefox extension教程

    https://developer.mozilla.org/zh-CN/docs/Add-ons/Overlay_Extensions/XUL_School/The_Essentials_of_an_ ...