您好,欢迎访问一九零五行业门户网

Python单元测试框架unittest简明使用实例

测试步骤
1. 导入unittest模块
import unittest
2. 编写测试的类继承unittest.testcase
class tester(unittest.testcase)
3. 编写测试的方法必须以test开头
def test_add(self)
def test_sub(self)
4.使用testcase class提供的方法测试功能点
5.调用unittest.main()方法运行所有以test开头的方法
复制代码 代码如下:
if __name__ == '__main__':
unittest.main()
实例如下
被测试类
复制代码 代码如下:
#!/usr/bin/python
#coding=utf-8class computer(object):
 @staticmethod
 def add(a, b):
  return a + b;
@staticmethod
 def sub(a, b):
  return a - b;
测试类
复制代码 代码如下:
#!/usr/bin/python
#coding=utf-8
import unittest
from testee import computerclass tester(unittest.testcase): 
 def test_add(self):
  self.assertequal(computer.add(2, 3), 5, test add function)
def test_sub(self):
  self.assertequal(computer.sub(5, 1), 4, test sub function) 
if __name__ == '__main__':
  unittest.main()
​运行结果:
复制代码 代码如下:
----------------------------------------------------------------------
ran 2 tests in 0.000s
ok
其它类似信息

推荐信息