Pythonnose_parameterized使unitTest支持参数化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pythonnose_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 of unittest.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

 

以上是关于Pythonnose_parameterized使unitTest支持参数化的主要内容,如果未能解决你的问题,请参考以下文章

“第二信使”都有哪些?与它作用的物质都有哪些?

使引导容器大小变大会使页面无响应

Css使Div自适应居中

如何用word使图片上下居中

如何使 RelativeLayout 半透明但不使活动

如何使图像自动调整大小,使宽度为 100% 并相应调整高度?