1. 对类进行单元检测:
第一步:首先编写一个类:
# 类名Person,person.py class Person(object): def __init__(self,name,age): self.name = name self.age = age def getAge(self): return self.age
第二部分:编写单元检测类进行类检测:
import unittest from person import Person class Test(unittest.TestCase): def test_init(self): p = Person("hanmeimei",20) self.assertEqual(p.name,"hanmeimei","属性赋值有误") def test_getAge(self): p = Person("hanmeimei",22) self.assertEqual(p.getAge(),p.age,"getAge函数有误") if __name__ == '__main__': unittest.main()