【Python】nose_parameterized使unitTest,nose支持参数化

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
  •  NOSE中使用
import nose
from nose_parameterized import  parameterized
from nose.tools import eq_


class  test_classs2():
    @parameterized.expand([
        ("1st", 4, 5),
        ("2en", 2, 4),
        ("3rd", 3, 4),
    ])
    def test_floor(self, name, source, expected):
        eq_(source + 1, expected)

if __name__ == "__main__":
    nose.runmodule()
    #nose.run()

result

C:\PycharmProjects\nose1\src>nosetests -v -s test_case.test_para
ALL starting...*****************
test_case.test_para.test_classs2.test_floor_0_1st ... ok
test_case.test_para.test_classs2.test_floor_1_2en ... FAIL
test_case.test_para.test_classs2.test_floor_2_3rd ... ok
ALL tearDown...*****************

======================================================================
FAIL: test_case.test_para.test_classs2.test_floor_1_2en
----------------------------------------------------------------------
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
  File "c:\python27\lib\site-packages\nose_parameterized\parameterized.py", line 365, in standalone_func
    return func(*(a + p.args), **p.kwargs)
  File "C:\PycharmProjects\nose1\src\test_case\test_para.py", line 13, in test_floor
    eq_(source + 1, expected)
AssertionError: 3 != 4

----------------------------------------------------------------------
Ran 3 tests in 0.076s

FAILED (failures=1)

 

转载于:https://www.cnblogs.com/AlexBai326/p/6169982.html

你可能感兴趣的:(测试,python,git)