楚阿旗

博客园 首页 新随笔 联系 订阅 管理
 1 from unittest import TestCase
 2 from triangle import area_of_a_triangle
 3 
 4 class TestAreaOfTriangle(TestCase):
 5 
 6     def test_float_values(self):
 7         """ Test areas when values are floats """
 8         self.assertAlmostEqual(area_of_a_triangle(3.4556, 8.3567), 14.43870626)
 9         self.assertEqual(area_of_a_triangle(2.3, 5.7), 6.555)
10 
11     def test_integer_values(self):
12         """ Test areas when values are integers """
13         self.assertEqual(area_of_a_triangle(2, 5), 5.0)
14         self.assertEqual(area_of_a_triangle(4, 6), 12.0)
15 
16     def test_zero_base(self):
17         """ Test areas when base is zero """
18         self.assertEqual(area_of_a_triangle(0, 5), 0.0)
19 
20     def test_zero_height(self):
21         """ Test areas when height is zero """
22         self.assertEqual(area_of_a_triangle(2, 0), 0.0)
23 
24     def test_zero_values(self):
25         """ Test areas when base and height are zero """
26         self.assertEqual(area_of_a_triangle(0, 0), 0.0)
27 
28     def test_negative_base(self):
29         """ Test that ValueError is raised when base is negative """
30         self.assertRaises(ValueError, area_of_a_triangle, -2, 5)
31 
32     def test_negative_height(self):
33         """ Test that ValueError is raised when height is negative """
34         self.assertRaises(ValueError, area_of_a_triangle, 2, -5)
35 
36     def test_negative_values(self):
37         """ Test that ValueError is raised when both are negative """
38         self.assertRaises(ValueError, area_of_a_triangle, -2, -5)
39 
40     def test_with_boolean(self):
41         """ Test that TypeError is raised with boolean types """
42         self.assertRaises(TypeError, area_of_a_triangle, True, 5)   # test booleans
43         self.assertRaises(TypeError, area_of_a_triangle, 2, True)
44 
45     def test_with_string(self):
46         """ Test that TypeError is raised with string types """
47         self.assertRaises(TypeError, area_of_a_triangle, "base", 5) # test strings
48         self.assertRaises(TypeError, area_of_a_triangle, 2, "height")
49 
50     def test_with_nulls(self):
51         """ Test that TypeError is raised with null types """
52         self.assertRaises(TypeError, area_of_a_triangle, None, 5) # test strings
53         self.assertRaises(TypeError, area_of_a_triangle, 2, None)

 

posted on 2024-04-20 18:43  楚阿旗  阅读(4)  评论(0编辑  收藏  举报