zoukankan      html  css  js  c++  java
  • python之parameterized模块

     parameterized扩展了py.test参数化测试,unittest参数化测试。

     <1>一个小练习

    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)
    
    class TestMathUnitTest(unittest.TestCase):
        @parameterized.expand([
            ("negative", -1.5, -2.0),
            ("integer", 1, 1.0),
            ("large fraction", 1.6, 1),
        ])
        def test_floor(self, name, input, expected):
            assert_equal(math.floor(input), expected)
    View Code

    用unittest运行,结果如下:

    D:pythonstudyWonderStitch>python3 -m unittest -v test
    test_floor_0_negative (test.TestMathUnitTest) ... ok
    test_floor_1_integer (test.TestMathUnitTest) ... ok
    test_floor_2_large_fraction (test.TestMathUnitTest) ... ok
    
    ----------------------------------------------------------------------
    Ran 3 tests in 0.002s
    
    OK
    View Code

    用nose运行,结果如下:

    D:pythonstudyWonderStitch>nosetests -v test.py
    test_floor_0_negative (test.TestMathUnitTest) ... ok
    test_floor_1_integer (test.TestMathUnitTest) ... ok
    test_floor_2_large_fraction (test.TestMathUnitTest) ... ok
    test.test_pow(2, 2, 4) ... ok
    test.test_pow(2, 3, 8) ... ok
    test.test_pow(1, 9, 1) ... ok
    test.test_pow(0, 9, 0) ... ok
    
    ----------------------------------------------------------------------
    Ran 7 tests in 0.004s
    
    OK
    View Code

    注意:因为UNITTEST不支持测试装饰器,故只有使用@parameterized.expand创建的测试才会被执行。

    >>>>>待续

  • 相关阅读:
    微信开发-Jssdk调用分享实例
    软件工程
    Homework_4 四则运算
    Homework 3
    每周总结
    自动生成四则运算题目
    Homework 1 -- The beginning
    程序员视角的餐饮行业
    iOS9网络适配_ATS:改用更安全的HTTPS
    Xcode的 发展史
  • 原文地址:https://www.cnblogs.com/wuxunyan/p/9531685.html
Copyright © 2011-2022 走看看