pytest和nose的多维参数化测试
testdimensions的Python项目详细描述
用python编写参数化测试有多种方法。unittest有 现在有一些支持,nose允许产生测试用例,pytest内置了 参数化支持,优秀的nose_parameterized包增强了 大多数测试框架中的这些功能。
testdimensions提供了编写多维测试的方便方法 一些简单场景中的矩阵。如果函数接受多个参数 你想测试一组参数组合的交叉积, testdimensions是给你的。
将测试指定为一个表,其:
- 单元格值是预期值(最后一个参数)
- 列由两个空格分隔 (确保所有行都是这样)
# 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.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 | not tested | yes | not tested | not tested | yes | not tested |
unittest | no | no | no | no | no | no |
unittest2 | no | no | no | no | no | no |
依赖关系
- nose_parameterized用于机头支撑
详尽的用法示例
@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一样。