unittest 提供了一组测试函数,可以用来特化测试,如assertDictEqual、assertEqual、assertTrue、assertFalse、assertGreater、assertGreaterEqual、assertIn、assertIs、assertIsIntance、assertIsNon、assertualIsNot、assertIsNotNone、assertItemsEqual、assertLess、assertLessEqual、assertListEqual、assertMultiLineEqual、assertNotAlmostEqual、assertNot-Equal、assertTupleEqual、assertRaises、assertRaisesRegexp、assertRegexpMatches
等
cap.py
def just_do_it(text): return text.capitalize()
test_cap.py
import unittest import cap class TestCap(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_one_word(self): text = 'duck' result = cap.just_do_it(text) self.assertEqual(result, 'Duck') def test_multiple_words(self): text = 'a veritable flock of ducks' result = cap.just_do_it(text) self.assertEqual(result, 'A Veritable Flock Of Ducks') if __name__ == '__main__': unittest.main()
python test_cap.py