上代码:
#!/usr/bin/env python
# coding=utf-8
class Count:
def __init__(self, a, b):
self.a = int(a)
self.b = int(b)
def add(self):
return self.a + self.b
import unittest
from count import Count
import unittest
from count import Count
# 测试两个整数相加,创建一个继承unittest.TestCase的测试类
class TestCount01(unittest.TestCase):
# 测试方法需要以test开头
def test_add(self):
# try:
# # 调用count模块中的Count类,并实例一个cot对象,传入两个实参3和4
# cot = Count(3, 4)
# # 调用Count类中的加法方法,并将值返回给变量add
# add = cot.add()
# # assert语法:assert 表达式[,异常参数],异常参数会传给except中定义的msg变量
# assert add == 6, "计算错误"
#
# except AssertionError as msg:
# print(msg)
#
# else:
# print("Test Pass")
cot = Count(3, 4)
add = cot.add()
self.assertEqual(add, 7, '计算错误')
if __name__ == "__main__":
# unittest.main()方法会查找类TestCount01中test开头的方法并执行
unittest.main()