首页 > 代码库 > 【Python】nose_parameterized使unitTest支持参数化
【Python】nose_parameterized使unitTest支持参数化
nose_parameterized使unitTest支持参数化
GIT https://github.com/wolever/nose-parameterized
- And
@parameterized.expand
can be used to generate test methods in situations where test generators cannot be used (for example, when the test class is a subclass ofunittest.TestCase
): - 使用unittest执行case
#nose_parameterized_demo import unittest from nose_parameterized import parameterized class TestMathUnitTest(unittest.TestCase): @parameterized.expand([ ("1st", 4, 5), ("2en", 2, 4), ("3rd", 3, 4), ]) def test_floor(self, name, source, expected): self.assertEqual(source + 1, expected)
运行结果
D:\workspace\pyUnitTest\src>python -m unittest -v nose_parameterized_demo test_floor_0_1st (nose_parameterized_demo.TestMathUnitTest) ... ok test_floor_1_2en (nose_parameterized_demo.TestMathUnitTest) ... FAIL test_floor_2_3rd (nose_parameterized_demo.TestMathUnitTest) ... ok ====================================================================== FAIL: test_floor_1_2en (nose_parameterized_demo.TestMathUnitTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Python27\lib\site-packages\nose_parameterized-0.5.0-py2.7.egg\nose_parameterized\parameterized.py", line 365, in standalone_func return func(*(a + p.args), **p.kwargs) File "nose_parameterized_demo.py", line 13, in test_floor self.assertEqual(source + 1, expected) AssertionError: 3 != 4 ---------------------------------------------------------------------- Ran 3 tests in 0.029s FAILED (failures=1) D:\workspace\pyUnitTest\src>
@parameterized.expand
通过变量引用参数化数据-
@parameterized.expand(input=params)附带的变量名称需为input
#nose_parameterized_demo import unittest from nose_parameterized import parameterized class TestMathUnitTest(unittest.TestCase): params = [ ("1st", 4, 5), ("2en", 2, 4), ("3rd", 3, 4), ] @parameterized.expand(input=params) def test_floor(self, name, source, expected): self.assertEqual(source + 1, expected)
- The
@parameterized
decorator can be used test class methods, and standalone functions: - 使用nosetest执行case
from nose.tools import assert_equal from nose_parameterized import parameterized 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)
执行结果
D:\workspace\pyUnitTest\src> nosetests -v t1.py t1.test_pow(2, 2, 4) ... ok t1.test_pow(2, 3, 8) ... ok t1.test_pow(1, 9, 1) ... ok t1.test_pow(0, 9, 0) ... ok ---------------------------------------------------------------------- Ran 4 tests in 0.025s OK
【Python】nose_parameterized使unitTest支持参数化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。