pytest和nose的多维参数化测试

testdimensions的Python项目详细描述


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
''')deftest_pow(base,exponent,expected):assertmath.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
''')deftest_round_floor_ceil(input,function,expected):assertfunction(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.6Py2.7Py3.3Py3.4Py3.5PyPy
nosenoyesnonoyesno
nose2nononononono
py.testnot testedyesnot testednot testedyesnot tested
unittestnononononono
unittest2nononononono

依赖关系

详尽的用法示例

@pytest_mark_dimensionsdecorator是 @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)deftest_add(a,b,expected):asserta+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)deftest_arithmetic_operations(operation,a,b,expected):assertoperation(a,b)==expected

对于鼻托,您需要安装nose_parameterized,并使用 @nosedimensionsdecorator:

@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)deftest_add(a,b,expected):asserta+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)deftest_arithmetic_operations(operation,a,b,expected):assertoperation(a,b)==expected

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
未检测到“空格”的java ASCII码   音频如何在Java中设置SourceDataLine的音量   java MyBatissSpringBoot无法使用作为bean注入的数据源   Java字谜递归列表<List<String>>仅存储空列表<Strings>   java显示30px高的JPanel,然后用minecraft填充剩余空间   java JOGL平滑度   Java:按完整与否对1d对象数组进行排序   arraylist仅当java结构不存在时才将对象添加到java结构中   无法初始化java渲染库在安卓 studio 4.1.1上出现错误   java SpringBoot ClassCastException   java解析文本文件,基于字数   java=运算符引发异常   java使点可拖动并存储新坐标JavaFx   JavaWebSphereApplicationServer:EJB模块和JMS路由器