python之parameterized模块

 parameterized扩展了py.test参数化测试,unittest参数化测试。

 <1>一个小练习

import unittest
import math

@parameterized([
    (2, 2, 4),
    (2, 3, 8),
    (1, 9, 1),
    (0, 9, 0),
])
def test_pow(base, exponent, expected):
    assert_equal(math.pow(base, exponent), expected)

class TestMathUnitTest(unittest.TestCase):
    @parameterized.expand([
        ("negative", -1.5, -2.0),
        ("integer", 1, 1.0),
        ("large fraction", 1.6, 1),
    ])
    def test_floor(self, name, input, expected):
        assert_equal(math.floor(input), expected)
View Code

用unittest运行,结果如下:

D:\pythonstudy\WonderStitch>python3 -m unittest -v test
test_floor_0_negative (test.TestMathUnitTest) ... ok
test_floor_1_integer (test.TestMathUnitTest) ... ok
test_floor_2_large_fraction (test.TestMathUnitTest) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK
View Code

用nose运行,结果如下:

D:\pythonstudy\WonderStitch>nosetests -v test.py
test_floor_0_negative (test.TestMathUnitTest) ... ok
test_floor_1_integer (test.TestMathUnitTest) ... ok
test_floor_2_large_fraction (test.TestMathUnitTest) ... ok
test.test_pow(2, 2, 4) ... ok
test.test_pow(2, 3, 8) ... ok
test.test_pow(1, 9, 1) ... ok
test.test_pow(0, 9, 0) ... ok

----------------------------------------------------------------------
Ran 7 tests in 0.004s

OK
View Code

注意:因为UNITTEST不支持测试装饰器,故只有使用@parameterized.expand创建的测试才会被执行。

 

>>>>>待续

posted @ 2018-08-24 19:14  enjoyzier  阅读(6190)  评论(0编辑  收藏  举报