1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| from unittest import TestCase import unittest
def average(values): return sum(values) / len(values)
def sumfun(values): return sum(values)
class TestAverage(TestCase): ''' 对求平均函数的测试用例 ''' def test_average1(self): self.assertEqual(average([20, 30, 70]), 40.0) self.assertEqual(round(average([1, 5, 7]), 0), 4) def test_average2(self): self.assertEqual(average([20, 30, 70]), 40.0) self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
def test_average3(self): data = [1, 2, 3, 4, 5, 6] self.assertEqual(average(data), 3.5)
class TestSum(TestCase): ''' 对求和函数的测试用例 ''' def test_sum1(self): self.assertEqual(sumfun([20, 30, 70]), 120.0) def test_sum2(self): self.assertEqual(sumfun([1, 0.5, -10]), -8.5)
if __name__ == '__main__': test_suite = unittest.TestSuite() test_suite.addTest(TestSum('test_sum1')) test_suite.addTest(TestAverage('test_average1'))
myrunner = unittest.TextTestRunner(verbosity=2) myrunner.run(test_suite)
|