最近在看视频时,虫师简单提到了简化自动化测试脚本用例中的代码量,而python中本身的参数化方法用来测试很糟糕,他在实际操作中使用了parameterized参数化...
有兴趣就查了下使用的方法,来分享给大家,使用Python测试框架进行参数化测试 下载安装https://github.com/wolever/parameterized或PIP install: $ pip install parameterized
parameterized了修正对于一切nose参数化测试,py.test参数化测试,单元测试参数化测试。
#test_math.py
from nose.tools importassert_equalfrom parameterized importparameterizedimportunittestimportmath
@parameterized([
(2, 2, 4),
(2, 3, 8),
(1, 9, 1),
(0,9, 0),
])deftest_pow(base, exponent, expected):
assert_equal(math.pow(base, exponent), expected)classTestMathUnitTest(unittest.TestCase):
@parameterized.expand([
("negative", -1.5, -2.0),
("integer", 1, 1.0),
("large fraction", 1.6, 1),
])deftest_floor(self, name, input, expected):
assert_equal(math.floor(input), expected)
在 nose (andnose2)下运行:
$ nosetests-v test_math.py
test_math.test_pow(2, 2, 4) ... ok
test_math.test_pow(2, 3, 8) ... ok
test_math.test_pow(1, 9, 1) ... ok
test_math.test_pow(0,9, 0) ... ok
test_floor_0_negative (test_math.TestMathUnitTest) ... ok
test_floor_1_integer (test_math.TestMathUnitTest) ... ok
test_floor_2_large_fraction (test_math.TestMathUnitTest) ... ok----------------------------------------------------------------------Ran7 tests in0.002s
OK
As the package name suggests, noseis best supported and will be used forall further