您的位置:首页 > 编程语言 > Python开发

python基础教程学习笔记十六

2016-10-12 11:52 696 查看
测试
测试工具:
Unitest 通用测试框架
示例代码如下:
#unittest框架的简单测试
import unittest,my_math
 
class PrductTestCase(unittest.TestCase):
 
    def testIntegers(self):
        for x in range(-10,10):
            for y in range(-10,10):
                p=my_math.product(x,y)
                self.assertTrue(p==x*y,'Integer multiplication failed')
 
    def testFloats(self):
        for x in range(-10,10):
               for y in range(-10,10):
                   x=x/10.0
                   y=y/10.0
                   p=my_math.product(x,y)
                   self.assertTrue(p==x*y,'Float multiplication failed')
 
if __name__=='__main__':
    unittest.main()
 
测试正确结果如下:
..
----------------------------------------------------------------------
Ran 2 tests in 0.004s
 
OK
Traceback (most recent call last):
  File "D:/workspace_python/unittest/unit_test.py", line 21, in <module>
    unittest.main()
  File "D:\Python32\lib\unittest\main.py", line 124, in __init__
    self.runTests()
  File "D:\Python32\lib\unittest\main.py", line 272, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: False
 
测试错误结果如下:
FF
======================================================================
FAIL: testFloats (__main__.PrductTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:/workspace_python/unittest/unit_test.py", line 18, in testFloats
    self.assertTrue(p==x*y,'Float multiplication failed')
AssertionError: False is not true : Float multiplication failed
 
======================================================================
FAIL: testIntegers (__main__.PrductTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:/workspace_python/unittest/unit_test.py", line 10, in testIntegers
    self.assertTrue(p==x*y,'Integer multiplication failed')
AssertionError: False is not true : Integer multiplication failed
 
----------------------------------------------------------------------
Ran 2 tests in 0.007s
 
FAILED (failures=2)
Traceback (most recent call last):
  File "D:/workspace_python/unittest/unit_test.py", line 21, in <module>
    unittest.main()
  File "D:\Python32\lib\unittest\main.py", line 124, in __init__
    self.runTests()
  File "D:\Python32\lib\unittest\main.py", line 272, in runTests
    sys.exit(not self.result.wasSuccessful())
SystemExit: True
>>>
 
常用的testCase方法
Assert_() 表示假则为失败
failUnless()
assertEqual()
failUnlessEqual()
assertNotEqual()
failIfEqual()
assertAlmostEqual(0
 
 
单元测试以外的内容
使用pyChecker和PyLint检查源代码
分析
 
 
Doctest 检查文档
示例代码如下:
#如果要测试my_python.py,可以在最后添加
#求数字的平方
def square(x):
    '''
    Aquare a number and returns the result.
    >>> square(2)
    4
    >>> square(3)
    9
    '''
    return x*x
if __name__=='__main__':
    import doctest,my_math
    doctest.testmod(my_math)
在命令行运行时可以设定脚本
Python my_python -v
 
显示结果如下:
D:\workspace_python\unittest>ls
__pycache__  my_math.py  unit_test.py
 
D:\workspace_python\unittest>python my_math.py -v
Trying:
    square(2)
Expecting:
    4
ok
Trying:
    square(3)
Expecting:
    9
ok
1 items had no tests:
    my_math
1 items passed all tests:
   2 tests in my_math.square
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
 
如果在文档注释中少写了一个2
运行时显示结果如下:
Trying:
    square()
Expecting:
    4
**********************************************************************
File "D:\workspace_python\unittest\my_math.py", line 5, in my_math.square
Failed example:
    square()
Exception raised:
    Traceback (most recent call last):
      File "D:\Python32\lib\doctest.py", line 1288, in __run
        compileflags, 1), test.globs)
      File "<doctest my_math.square[0]>", line 1, in <module>
        square()
    TypeError: square() takes exactly 1 argument (0 given)
Trying:
    square(3)
Expecting:
    9
ok
1 items had no tests:
    my_math
**********************************************************************
1 items had failures:
   1 of   2 in my_math.square
2 tests in 2 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: