跳转到主要内容

Pytest和Nose的多维参数化测试

项目描述

https://travis-ci.org/akaihola/testdimensions.svg?branch=master

在Python中编写参数化测试有多种方法。Unittest最近提供了一些支持,Nose允许产生测试用例,Pytest有内置的参数化支持,而优秀的nose_parameterized包增强了大多数测试框架的这些功能。

testdimensions提供了一种方便的方法,在简单场景中编写多维测试矩阵。如果您的函数接受多个参数,并且想要测试参数组合的笛卡尔积,那么testdimensions就是为您准备的。

指定您的测试为一个表格,其中

  • Y轴标签是倒数第三个参数的值

  • X轴标签是倒数第二个参数的值

  • 单元格值是预期值(最后一个参数)

  • 列之间由两个空格分隔(确保所有行都如此)

# test_math.py
@pytest_mark_dimensions('base,exponent,expected', '''
    # y: base
    # x: exponent
    # cell: expected

          2    3    9
     0    0    0    0
     1    1    1    1
     2    4    8  512
''')
def test_pow(base, exponent, expected):
    assert math.pow(base, exponent) == expected


@pytest_mark_dimensions('input,function,expected', '''
                round  math.floor  math.ceil
    -1.5         -2.0        -2.0       -1.0
     1.0          1.0         1.0        1.0
     1.6          2.0         1.0        2.0
''')
def test_round_floor_ceil(input, function, expected):
    assert function(input) == expected

输出

$ pytest -v
=========================== test session starts ===============================
platform linux -- Python 3.5.2, pytest-3.0.3, py-1.4.31, pluggy-0.4.0
collected 18 items

test_math.py::test_pow[0-2-0] PASSED
test_math.py::test_pow[0-3-0] PASSED
test_math.py::test_pow[0-9-0] PASSED
test_math.py::test_pow[1-2-1] PASSED
test_math.py::test_pow[1-3-1] PASSED
test_math.py::test_pow[1-9-1] PASSED
test_math.py::test_pow[2-2-4] PASSED
test_math.py::test_pow[2-3-8] PASSED
test_math.py::test_pow[2-9-512] PASSED
test_math.py::test_round_floor_ceil[-1.5-function0--2.0] PASSED
test_math.py::test_round_floor_ceil[-1.5-function1--2.0] PASSED
test_math.py::test_round_floor_ceil[-1.5-function2--1.0] PASSED
test_math.py::test_round_floor_ceil[1.0-function3-1.0] PASSED
test_math.py::test_round_floor_ceil[1.0-function4-1.0] PASSED
test_math.py::test_round_floor_ceil[1.0-function5-1.0] PASSED
test_math.py::test_round_floor_ceil[1.6-function6-2.0] PASSED
test_math.py::test_round_floor_ceil[1.6-function7-1.0] PASSED
test_math.py::test_round_floor_ceil[1.6-function8-2.0] PASSED

============================ 18 passed in 0.03 seconds ========================

安装

$ pip install testdimensions

兼容性

Py2.6

Py2.7

Py3.3

Py3.4

Py3.5

PyPy

nose

no

yes

no

no

yes

no

nose2

no

no

no

no

no

no

py.test

未测试

yes

未测试

未测试

yes

未测试

unittest

no

no

no

no

no

no

unittest2

no

no

no

no

no

no

依赖关系

详尽的用法示例

@pytest_mark_dimensions 装饰器是@pytest.mark.parametrize的扩展,其第一个参数是一个逗号分隔的测试参数列表。第二个参数是一个定义实际测试的多行字符串。您还可以使用关键字参数将值注入测试的全局命名空间。

要创建超过二维的测试,只需定义多个表,并在其中穿插额外参数的值。

@pytest_mark_dimensions('a,b,expected', '''
            -10   0   9  million
    -9      -19  -9   0   999991
     0      -10   0   9  million
    10        0  10  19  1000010
    ''',
    million=1000000)
def test_add(a, b, expected):
    assert a + b == expected


@pytest_mark_dimensions('operation,a,b,expected', '''
    operation = operator.sub

            -10   0    9   million
    -9        1  -9  -18  -1000009
     0       10   0   -9  -million
    10       20  10    1   -999990

    operation = operator.add

            -10   0   9  million
    -9      -19  -9   0   999991
     0      -10   0   9  million
    10        0  10  19  1000010

    operation = operator.mul

            -10   0    9   million
    -9       90   0  -81  -9000000
     0        0   0    0         0
    10     -100   0   90  10000000

    ''',
    million=1000000)
def test_arithmetic_operations(operation, a, b, expected):
    assert operation(a, b) == expected

对于Nose支持,您需要安装nose_parameterized并使用@nosedimensions装饰器

@nosedimensions('a,b,expected', '''
            -10   0   9  million
    -9      -19  -9   0   999991
     0      -10   0   9  million
    10        0  10  19  1000010
    ''',
    million=1000000)
def test_add(a, b, expected):
    assert a + b == expected


@nosedimensions('operation,a,b,expected', '''
    operation = operator.sub

            -10   0    9   million
    -9        1  -9  -18  -1000009
     0       10   0   -9  -million
    10       20  10    1   -999990

    operation = operator.add

            -10   0   9  million
    -9      -19  -9   0   999991
     0      -10   0   9  million
    10        0  10  19  1000010

    operation = operator.mul

            -10   0    9   million
    -9       90   0  -81  -9000000
     0        0   0    0         0
    10     -100   0   90  10000000

    ''',
    million=1000000)
def test_arithmetic_operations(operation, a, b, expected):
    assert operation(a, b) == expected

请注意,您仍然需要像使用Pytest一样枚举测试参数。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

testdimensions-0.0.1.tar.gz (4.7 kB 查看哈希值)

上传时间

支持