python基础教程学习笔记十六
来源:互联网 发布:mac装系统开机按哪个键 编辑:程序博客网 时间:2024/06/05 17:55
测试
测试工具:
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.
- python基础教程学习笔记十六
- python基础教程学习笔记
- python基础教程学习笔记
- Python基础教程学习笔记
- python 学习笔记(十六)
- Python基础教程学习笔记----第一章
- python基础教程学习笔记一
- python基础教程学习笔记二
- python基础教程学习笔记三
- python基础教程学习笔记四
- python基础教程学习笔记五
- python基础教程学习笔记六
- python基础教程学习笔记七
- python基础教程学习笔记八
- python基础教程学习笔记九
- python基础教程学习笔记十
- python基础教程学习笔记十一
- python基础教程学习笔记十二
- 将博客搬至CSDN
- 操作系统的进程状态及linux实例
- Java之枚举用法以及http,http/2的访问返回常识,google浏览器插件
- MySQL学习笔记8:MySQL存储过程(storage procedure)
- OWASP 测试指南 4.0-测试原则
- python基础教程学习笔记十六
- FMDB的简单封装
- RxJava 和 RxAndroid 四(RxBinding的使用)
- Linux中gdb 查看core堆栈信息
- Makefile和Android.mk的基情
- oracle异地恢复
- QMediaPlayer播放视频
- 函数指针与回调
- RxJava 和 RxAndroid 五(线程调度)