zoukankan      html  css  js  c++  java
  • 【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:workspacepyUnitTestsrc>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:Python27libsite-packages
    ose_parameterized-0.5.0-py2.7.egg
    ose_parameterizedparameterized.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:workspacepyUnitTestsrc>
    • @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:workspacepyUnitTestsrc> 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
    ose1src>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:python27libsite-packages
    osecase.py", line 197, in runTest
        self.test(*self.arg)
      File "c:python27libsite-packages
    ose_parameterizedparameterized.py", line 365, in standalone_func
        return func(*(a + p.args), **p.kwargs)
      File "C:PycharmProjects
    ose1src	est_case	est_para.py", line 13, in test_floor
        eq_(source + 1, expected)
    AssertionError: 3 != 4
    
    ----------------------------------------------------------------------
    Ran 3 tests in 0.076s
    
    FAILED (failures=1)
  • 相关阅读:
    aspnet mvc 中 跨域请求的处理方法
    Aspnet Mvc 前后端分离项目手记(三)关于restful 风格Url设计
    Aspnet Mvc 前后端分离项目手记(二)关于token认证
    Aspnet Mvc 前后端分离项目手记(一) 关于跨域问题(还有前言)
    31 | 误删数据后除了跑路,还能怎么办?
    30 | 答疑文章(二):用动态的观点看加锁
    29 | 如何判断一个数据库是不是出问题了?
    28 | 读写分离有哪些坑?
    27 | 主库出问题了,从库怎么办?
    26 | 备库为什么会延迟好几个小时?
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/6169982.html
Copyright © 2011-2022 走看看