parameterized擴展了p
y.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)
用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
用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
注意:因為UNITTEST不支持測試裝飾器,故只有使用@parameterized.expand創建的測試才會被執行。
>>>>>待續