對寫的函式或方法測試(非調介面方式)
方案1: 自己編寫測試類
方案2:用python自帶的unittest模組
案例:
import unittest class Student: def __init__(self, name, score): self.name = name self.score = score def get_score(self): if 80 > self.score >= 60: return 'B' if self.score > 80: return 'A' return 'C' class StudentTestcase(unittest.TestCase): def test_80_to_100(self): stu1 = Student('stu1', 90) stu2 = Student('stu2', 80) self.assertEqual(stu1.get_score(), 'A') self.assertEqual(stu2.get_score(), 'A') def test_60_to_80(self): stu3 = Student('stu3', 70) self.assertEqual(stu3.get_score(), 'B') def test_0_to_60(self): stu4 = Student('stu4', 50) self.assertEqual(stu4.get_score(), 'C') if __name__ == '__main__': unittest.main()
參考: 單元測試 - 廖雪峰的官方網站 (liaoxuefeng.com)
方案3:第三方測試模組