python 单元测试示例2
?
?
#!/usr/bin/env pythonimport unittest;class CValue: '''test for run equal''' def __init__(self, nVal): self.m_nVal = nVal; def Equal(self, nVal): if self.m_nVal == nVal: return True; return False; def Multiple(self, nVal): return 0 == self.m_nVal % nVal;class CValueTest(unittest.TestCase): def __init__(self, strMethod): #initilization parent unittest.TestCase.__init__(self, strMethod); print("init test case object!"); self.m_oVal = None; def setUp(self): self.m_oVal = CValue(100); def tearDown(self): print("end of test"); def testEqual(self): self.assertEqual(True, self.m_oVal.Equal(100)); self.assertEqual(True, self.m_oVal.Equal(101)); def testMultiple(self): print('test equal2'); self.assertEqual(True, self.m_oVal.Multiple(20)); self.assertEqual(False, self.m_oVal.Multiple(3)); self.assertEqual(True, self.m_oVal.Multiple(11));#create test collectiondef suite(): oSuite = unittest.TestSuite(); oSuite.addTest(CValueTest("testEqual")); oSuite.addTest(CValueTest("testMultiple")); return oSuite; if "__main__" == __name__: unittest.main(defaultTest = 'suite');