6.Parametrize Fixture

乙醇 创建于 over 6 years 之前

最后更新: about 1 month 之前

阅读数: 1

6.Parametrize 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 ========
0

相关课程

webium简明教程
图文
webium简明教程

课程分类: 测试框架

开箱即用的page object模式

  • 已完结
  • 已更新8集
  • 最后更新时间: 2024-03-18 12:48:12

免费

查看详情
TestNG教程
图文
TestNG教程

课程分类: 测试框架

Java语言中最流行的测试框架了

  • 已完结
  • 已更新12集
  • 最后更新时间: 2024-03-18 12:55:14

免费

查看详情
python unittest测试框架教程
图文
python unittest测试框架教程

课程分类: 测试框架

python 自带的单元测试框架

  • 已完结
  • 已更新8集
  • 最后更新时间: 2024-03-18 12:12:46

免费

查看详情
TDD测试驱动开发教程
图文
TDD测试驱动开发教程

课程分类: 测试框架 软件测试基础

TDD其实并不神秘

  • 已完结
  • 已更新7集
  • 最后更新时间: 2024-03-18 11:53:22

免费

查看详情