def test_get_rectangle_diagonal(self): given_data = [(1, 3), (4, 6), (7, 9), (4, 10)] for (given_width, given_height) in given_data: given_result = Rectangle(given_width, given_height).get_rectangle_diagonal() expected_result = math.sqrt( math.pow(given_height, 2) + math.pow(given_width, 2)) self.assertEqual(given_result, expected_result)
class TestRectangleClass(unittest.TestCase): def setUp(self): self.rectangle = Rectangle(width=3, height=4) def test_01_get_rectangle_perimeter(self): func_result = self.rectangle.get_rectangle_perimeter() self.assertEqual(func_result, 14) def test_02_get_rectangle_square(self): func_result = self.rectangle.get_rectangle_square() self.assertEqual(func_result, 12) def test_03_get_rectangle_diagonal(self): func_result = self.rectangle.get_rectangle_diagonal() self.assertEqual(func_result, 5) def test_04_get_sum_of_corners(self): params_for_corner = [(0, 0), (1, 90), (2, 180), (3, 270), (4, 360)] for corner, expected_result in params_for_corner: with self.subTest(corners=corner, expected_result=expected_result): self.assertEqual(self.rectangle.get_sum_of_corners(corner), expected_result) def test_05_get_sum_of_corners_error(self): self.assertRaises(ValueError, self.rectangle.get_sum_of_corners, 5) # TypeError def test_06_get_sum_of_corners_error_message(self): with self.assertRaises(ValueError) as err: self.rectangle.get_sum_of_corners(5) self.assertIn("Rectangle has only 4 corners", str(err.exception)) @patch("tests.homework.Rectangle.get_rectangle_diagonal", return_value=5) def test_07_get_radius_of_circumscribed_circle(self, mock): func_result = self.rectangle.get_radius_of_circumscribed_circle() self.assertEqual(func_result, 2.5) def test_08_get_radius_of_inscribed_circle(self): instance_rectangle = Rectangle(width=3, height=3) func_result = instance_rectangle.get_radius_of_inscribed_circle() self.assertEqual(func_result, 1.5) def test_09_get_radius_of_inscribed_circle_error(self): with self.assertRaises(ValueError): self.rectangle.get_radius_of_inscribed_circle()
def rectangle(): return Rectangle(width=3, height=4)
def test_equal_sides(self): with self.assertRaises(ValueError): rect = Rectangle(10, 15) rect.get_radius_of_inscribed_circle()
def test_get_radius_of_inscribed_circle(self): rect = Rectangle(10, 10) given_result = rect.get_radius_of_inscribed_circle() expected_result = rect.get_rectangle_diagonal() / (2 * math.sqrt(2)) self.assertEqual(given_result, expected_result)
def test_get_radius_of_circumscribed_circle(self): rect = Rectangle(10, 5) given_result = rect.get_radius_of_circumscribed_circle() expected_result = rect.get_rectangle_diagonal() / 2 self.assertEqual(given_result, expected_result)
def test_no_of_corners_more_than_4(self): with self.assertRaises(ValueError): Rectangle(10, 5).get_sum_of_corners(5)
def test_get_sum_of_corners(self): for no_of_corners in range(1, 5): given_result = Rectangle(10, 20).get_sum_of_corners(no_of_corners) expected_result = 90 * no_of_corners self.assertEqual(given_result, expected_result)
def test_get_rectangle_square(self): given_result = Rectangle(self.height, self.width).get_rectangle_square() expected_result = self.height * self.width self.assertEqual(given_result, expected_result)
def test_get_rectangle_perimeter(self): given_result = Rectangle(self.width, self.height).get_rectangle_perimeter() expected_result = (self.width + self.height) * 2 self.assertEqual(given_result, expected_result)
class TestRectangle(unittest.TestCase): rectangle_1 = Rectangle(3, 8) rectangle_2 = Rectangle(1.4, 4) rectangle_3 = Rectangle(4, 4) def test_get_rectangle_perimetr(self): self.assertEqual( self.rectangle_1.get_rectangle_perimeter(), ((self.rectangle_1.height + self.rectangle_1.width) * 2)) self.assertEqual( self.rectangle_2.get_rectangle_perimeter(), ((self.rectangle_2.height + self.rectangle_2.width) * 2)) def test_get_rectangle_square(self): self.assertEqual(self.rectangle_1.get_rectangle_square(), self.rectangle_1.height * self.rectangle_1.width) self.assertEqual(self.rectangle_2.get_rectangle_square(), self.rectangle_2.height * self.rectangle_2.width) def test_get_sum_of_corners_invalid_corners(self): for i in range(5, 10): with self.assertRaises(ValueError): self.rectangle_1.get_sum_of_corners(i) self.rectangle_2.get_sum_of_corners(i) def test_get_sum_of_corners(self): for i in range(1, 5): self.assertEqual(self.rectangle_1.get_sum_of_corners(i), i * 90) self.assertEqual(self.rectangle_2.get_sum_of_corners(i), i * 90) def test_get_rectangle_diagonal(self): self.assertEqual( self.rectangle_1.get_rectangle_diagonal(), math.sqrt( math.pow(self.rectangle_1.height, 2) + math.pow(self.rectangle_1.width, 2))) self.assertEqual( self.rectangle_2.get_rectangle_diagonal(), math.sqrt( math.pow(self.rectangle_2.height, 2) + math.pow(self.rectangle_2.width, 2))) def test_get_radius_of_circumscribed_circle(self): self.assertEqual(self.rectangle_1.get_radius_of_circumscribed_circle(), self.rectangle_1.get_rectangle_diagonal() / 2) self.assertEqual(self.rectangle_2.get_radius_of_circumscribed_circle(), self.rectangle_2.get_rectangle_diagonal() / 2) def test_get_radius_of_inscribed_circle_error(self): if self.rectangle_1.width != self.rectangle_1.height: with self.assertRaises(ValueError): self.rectangle_1.get_radius_of_inscribed_circle() def test_get_radius_of_inscribed_circle_pass(self): self.assertEqual( self.rectangle_3.get_radius_of_inscribed_circle(), self.rectangle_3.get_rectangle_diagonal() / 2 * math.sqrt(2)) def test_get_radius_of_inscribed_circle_failed(self): with self.assertRaises(ValueError): self.assertEqual( self.rectangle_1.get_radius_of_inscribed_circle(), self.rectangle_1.get_rectangle_diagonal() / (2 * math.sqrt(2)))
def setUp(self): self.rectangle = Rectangle(width=3, height=4)
def test_08_get_radius_of_inscribed_circle(self): instance_rectangle = Rectangle(width=3, height=3) func_result = instance_rectangle.get_radius_of_inscribed_circle() self.assertEqual(func_result, 1.5)
def test_09_get_radius_of_inscribed_circle_error(): rectangle = Rectangle(width=3, height=4) with pytest.raises(ValueError): rectangle.get_radius_of_inscribed_circle()
def test_08_get_radius_of_inscribed_circle(): rectangle = Rectangle(width=3, height=3) assert rectangle.get_radius_of_inscribed_circle() == 1.5