class RectangleTest(unittest.TestCase): def test_get_rectangle_perimeter(self): self.assertEqual(self.rect.get_rectangle_perimeter(), 8) def setUp(self): self.rect = Rectangle(2, 2) self.rect1 = Rectangle(3, 2) def tearDown(self): pass def test_rectangle_square(self): self.assertEqual(self.rect.get_rectangle_square(), 4) def test_get_sum_of_corner(self): number_of_corners = [0, 1, 2, 3, 4] sum_of_corner = [0, 90, 180, 270, 360] for i in range(len(number_of_corners)): with self.subTest(i=i): self.assertEqual(self.rect.get_sum_of_corners(i), sum_of_corner[i]) def test_get_rectangle_diagonal(self): for i in range(2, 10): dia = math.sqrt(math.pow(i, 2) + math.pow(i, 2)) self.rect = Rectangle(i, i) with self.subTest(i=i): self.assertEqual(self.rect.get_rectangle_diagonal(), dia) def test_get_radius_of_circumscribed_circle(self): self.assertEqual(self.rect.get_radius_of_circumscribed_circle(), (self.rect.get_rectangle_diagonal()) / 2) def test_get_radius_of_inscribed_circle(self): self.assertEqual(self.rect.get_radius_of_inscribed_circle(), self.rect.get_rectangle_diagonal() / (2 * math.sqrt(2))) self.assertRaises(ValueError, self.rect1.get_radius_of_inscribed_circle)
class RectangleValidValues(unittest.TestCase): def setUp(self): self.rect1 = Rectangle(3, 5) self.rect2 = Rectangle(3, 3) def test_get_rectangle_perimeter(self): self.assertEqual(self.rect1.get_rectangle_perimeter(), 16) self.assertEqual(self.rect2.get_rectangle_perimeter(), 12) def test_get_rectangle_square(self): self.assertEqual(self.rect1.get_rectangle_square(), 15) self.assertEqual(self.rect2.get_rectangle_square(), 9) def test_get_sum_of_corners(self): with self.subTest(): for num in range(1, 5): self.assertEqual(self.rect1.get_sum_of_corners(num), num * 90) def test_get_rectangle_diagonal(self): self.assertEqual(self.rect1.get_rectangle_diagonal(), math.sqrt(34)) self.assertEqual(self.rect2.get_rectangle_diagonal(), math.sqrt(18)) def test_get_radius_of_circumscribed_circle(self): self.assertEqual(self.rect1.get_radius_of_circumscribed_circle(), math.sqrt(34) / 2) self.assertEqual(self.rect2.get_radius_of_circumscribed_circle(), math.sqrt(18) / 2) def test_get_radius_of_inscribed_circle(self): self.assertEqual(self.rect2.get_radius_of_inscribed_circle(), math.sqrt(18) / (2 * math.sqrt(2)))
class RectangleTestCase(unittest.TestCase): def setUp(self): self.rectangle1 = Rectangle(2, 2) self.rectangle2 = Rectangle(4, 2) self.rectangle3 = Rectangle(2, 4) self.rectangle4 = Rectangle(2.2, 2.2) def test_get_rectangle_perimeter(self): self.assertEqual(self.rectangle1.get_rectangle_perimeter(), 8) self.assertEqual(self.rectangle2.get_rectangle_perimeter(), 12) self.assertEqual(self.rectangle3.get_rectangle_perimeter(), 12) def test_get_rectangle_square(self): self.assertEqual(self.rectangle1.get_rectangle_square(), 4) self.assertEqual(self.rectangle2.get_rectangle_square(), 8) self.assertEqual(self.rectangle3.get_rectangle_square(), 8) def test_get_sum_of_corners(self): for i in range(0, 5): self.assertEqual(self.rectangle1.get_sum_of_corners(i), i * 90) self.assertEqual(self.rectangle2.get_sum_of_corners(i), i * 90) self.assertEqual(self.rectangle3.get_sum_of_corners(i), i * 90) def test_get_sum_of_corners_error(self): with self.assertRaises(ValueError): self.rectangle1.get_sum_of_corners(5) self.rectangle2.get_sum_of_corners(5) self.rectangle3.get_sum_of_corners(5) def test_get_rectangle_diagonal(self): self.assertEqual(self.rectangle1.get_rectangle_diagonal(), sqrt(8)) self.assertEqual(self.rectangle2.get_rectangle_diagonal(), sqrt(20)) self.assertEqual(self.rectangle3.get_rectangle_diagonal(), sqrt(20)) def test_get_radius_of_circumscribed_circle(self): self.assertEqual( self.rectangle1.get_radius_of_circumscribed_circle(), sqrt(8) / 2) self.assertEqual( self.rectangle2.get_radius_of_circumscribed_circle(), sqrt(20) / 2) self.assertEqual( self.rectangle3.get_radius_of_circumscribed_circle(), sqrt(20) / 2) def test_get_radius_of_inscribed_circle(self): self.assertEqual( self.rectangle1.get_radius_of_inscribed_circle(), sqrt(pow(2, 2) + pow(2, 2)) / (2 * sqrt(2))) self.assertEqual( self.rectangle4.get_radius_of_inscribed_circle(), sqrt(pow(2.2, 2) + pow(2.2, 2)) / (2 * sqrt(2))) with self.assertRaises(ValueError): self.rectangle2.get_radius_of_inscribed_circle() self.rectangle3.get_radius_of_inscribed_circle()
class TestRectangle(unittest.TestCase): def setUp(self) -> None: self.t_rect = Rectangle self.t_norm_rect = Rectangle(2, 4) self.t_sqr_rect = Rectangle(2, 2) self.t_norm_rect_diagonal = math.sqrt( math.pow(self.t_norm_rect.height, 2) + math.pow(self.t_norm_rect.width, 2)) self.t_sqr_rect_diagonal = math.sqrt( math.pow(self.t_sqr_rect.height, 2) + math.pow(self.t_sqr_rect.width, 2)) self.t_sqr_rad_of_inscribed_circle =\ self.t_sqr_rect_diagonal / (2 * math.sqrt(2)) def tearDown(self) -> None: del self.t_rect def test_get_rectangle_perimeter(self): self.assertEqual(self.t_norm_rect.get_rectangle_perimeter(), 12) def test_get_rectangle_square(self): self.assertEqual(self.t_sqr_rect.get_rectangle_square(), 4) def test_get_sum_of_corners_success(self): for i in range(1, 5): with self.subTest(i=i): self.assertEqual(self.t_norm_rect.get_sum_of_corners(i), i * 90) def test_get_sum_of_corners_error(self): for i in range(5, 100): with self.subTest(i=i): with self.assertRaises(ValueError): self.t_norm_rect.get_sum_of_corners(i) def test_get_rectangle_diagonal(self): self.assertEqual(self.t_norm_rect.get_rectangle_diagonal(), self.t_norm_rect_diagonal) self.assertEqual(self.t_sqr_rect.get_rectangle_diagonal(), self.t_sqr_rect_diagonal) def test_get_radius_of_circumscribed_circle(self): self.assertEqual(self.t_norm_rect.get_radius_of_circumscribed_circle(), self.t_norm_rect_diagonal / 2) self.assertEqual(self.t_sqr_rect.get_radius_of_circumscribed_circle(), self.t_sqr_rect_diagonal / 2) def test_get_radius_of_inscribed_circle_success(self): self.assertEqual(self.t_sqr_rect.get_radius_of_inscribed_circle(), self.t_sqr_rad_of_inscribed_circle) def test_get_radius_of_inscribed_circle_error(self): with self.assertRaises(ValueError): self.t_norm_rect.get_radius_of_inscribed_circle()
class Test(unittest.TestCase): def setUp(self): self.width, self.height = 2, 3 self.rectangle = Rectangle(self.width, self.height) self.diagonal = self.rectangle.get_rectangle_diagonal() def test_rect_perim(self): act_res = self.rectangle.get_rectangle_perimeter() exp_res = ((self.width + self.height) * 2) self.assertEqual(act_res, exp_res) def test_rectangle_square(self): act_res = self.rectangle.get_rectangle_square() exp_res = self.width * self.height self.assertEqual(act_res, exp_res) def test_get_sum_of_corners_valid(self): rectangle = self.rectangle.get_sum_of_corners(3) exp_res = 3 * 90 act_res = rectangle self.assertEqual(act_res, exp_res) def test_get_sum_of_corners_invalid(self): with self.assertRaises(ValueError): self.rectangle.get_sum_of_corners(5) @unittest.skip("Will fail") def test_get_sum_of_corners_invalid2(self): with self.assertRaises(ValueError): self.rectangle.get_sum_of_corners(0) def test_get_rectangle_diagonal_valid(self): from math import sqrt act_res = self.rectangle.get_rectangle_diagonal() exp_res = sqrt(2 ** 2 + 3 ** 2) self.assertEqual(act_res, exp_res) def test_radius_of_circumscribed_circle(self): exp_res = self.diagonal / 2 act_res = self.rectangle.get_radius_of_circumscribed_circle() self.assertEqual(exp_res, act_res) def test_radius_of_inscribed_circle_valid(self): act_res = Rectangle(4, 4).get_radius_of_inscribed_circle() exp_res = 4 / 2 self.assertEqual(act_res, exp_res) def test_radius_of_inscribed_circle_invalid(self): with self.assertRaises(ValueError): Rectangle(self.width, self.height).get_radius_of_inscribed_circle()
class TestForRectangle(unittest.TestCase): def setUp(self) -> None: self.rectangle1 = Rectangle(6, 8) self.rectangle2 = Rectangle(6, 6) self.rectangle3 = Rectangle(0, 0) def test_get_rectangle_perimeter(self): self.assertEqual(self.rectangle1.get_rectangle_perimeter(), 28) self.assertEqual(self.rectangle2.get_rectangle_perimeter(), 24) self.assertEqual(self.rectangle3.get_rectangle_perimeter(), 0) def test_get_rectangle_square(self): self.assertEqual(self.rectangle1.get_rectangle_square(), 48) self.assertEqual(self.rectangle2.get_rectangle_square(), 36) self.assertEqual(self.rectangle3.get_rectangle_square(), 0) def test_get_sum_of_corners(self): self.assertEqual(self.rectangle1.get_sum_of_corners(4), 360) self.assertEqual(self.rectangle1.get_sum_of_corners(1), 90) def test_get_sum_of_corners_false(self): with self.assertRaises(ValueError): self.rectangle1.get_sum_of_corners(5) self.rectangle1.get_sum_of_corners(0) self.rectangle1.get_sum_of_corners(-4) def test_get_rectangle_diagonal(self): self.assertEqual(round(self.rectangle1.get_rectangle_diagonal(), 10), 10.0) self.assertEqual(round(self.rectangle2.get_rectangle_diagonal(), 8), 8.48528137) def test_get_radius_of_circumscribed_circle(self): self.assertEqual( round(self.rectangle2.get_radius_of_circumscribed_circle(), 4), 4.2426) def get_radius_of_circumscribed_circle_false(self): with self.assertRaises(ValueError): self.rectangle1.get_radius_of_circumscribed_circle() self.rectangle3.get_radius_of_circumscribed_circle() def test_radius_of_inscribed_circle(self): self.assertEqual(self.rectangle2.get_radius_of_inscribed_circle(), math.sqrt(72) / (2 * math.sqrt(2))) def get_radius_of_inscribed_circle_false(self): with self.assertRaises(ValueError): self.rectangle1.get_radius_of_inscribed_circle() with self.assertRaises(ValueError): self.rectangle3.get_radius_of_inscribed_circle()
class Test(unittest.TestCase): def setUp(self): self.width, self.height = 4, 6 self.rectangle = Rectangle(self.width, self.height) def test_1_rectangle_perimeter(self): cheack = (self.width + self.height) * 2 result = self.rectangle.get_rectangle_perimeter() self.assertEqual(cheack, result) def test_2_rectangle_square(self): cheack = self.width * self.height result = self.rectangle.get_rectangle_square() self.assertEqual(cheack, result) def test_3_sum_of_corners_valid(self): cheack = 4 * 90 result = self.rectangle.get_sum_of_corners(4) self.assertEqual(cheack, result) def test_4_sum_of_corners_invalid(self): with self.assertRaises(ValueError): self.rectangle.get_sum_of_corners(6) def test_5_sum_of_corners_invalid(self): with self.assertRaises(ValueError): self.rectangle.get_sum_of_corners(0) def test_6_rectangle_diagonal(self): cheack = sqrt(self.width**2 + self.height**2) result = self.rectangle.get_rectangle_diagonal() self.assertEqual(cheack, result) def test_7_radius_of_circumscribed_circle(self): cheack = self.rectangle.get_rectangle_diagonal() / 2 result = self.rectangle.get_radius_of_circumscribed_circle() self.assertEqual(cheack, result) def test_8_radius_of_inscribed_circle(self): rectangle = Rectangle(width=1, height=1) cheack = rectangle.get_rectangle_diagonal() / 2 * sqrt(2) result = rectangle.get_radius_of_inscribed_circle() self.assertEqual(cheack, result) def test_9_radius_of_inscribed_circle(self): with self.assertRaises(ValueError): return self.rectangle.get_radius_of_inscribed_circle()
class TestRectangle(unittest.TestCase): def setUp(self): self.rec = Rectangle(3, 8) self.rec1 = Rectangle(4, 4) def test_get_rectangle_perimeter(self): func_result = self.rec.get_rectangle_perimeter() func_result1 = self.rec1.get_rectangle_perimeter() self.assertEqual(func_result, 22) self.assertEqual(func_result1, 16) def test_get_rectangle_square(self): func_result = self.rec.get_rectangle_square() func_result1 = self.rec1.get_rectangle_square() self.assertEqual(func_result, 24) self.assertEqual(func_result1, 16) def test_get_sum_of_corners(self): func_result = self.rec.get_sum_of_corners(2) self.assertEqual(func_result, 180) def test_get_sum_of_corners_value_error(self): self.assertRaises(ValueError, self.rec.get_sum_of_corners, 5) def test_get_rectangle_diagonal(self): func_result = self.rec.get_rectangle_diagonal() func_result1 = self.rec1.get_rectangle_diagonal() self.assertAlmostEqual(func_result, 8.54400374531753) self.assertAlmostEqual(func_result1, 5.656854249492381) def test_get_radius_of_circumscribed_circle(self): func_result = self.rec.get_radius_of_circumscribed_circle() func_result1 = self.rec1.get_radius_of_circumscribed_circle() self.assertAlmostEqual(func_result, 8.54400374531753/2) self.assertAlmostEqual(func_result1, 5.656854249492381/2) def test_get_radius_of_inscribed_circle_error(self): self.assertRaises(ValueError, self.rec.get_radius_of_inscribed_circle) def test_get_radius_of_inscribed_circle(self): func_result = self.rec1.get_radius_of_inscribed_circle() self.assertEqual(func_result, 2)
class Test(unittest.TestCase): def setUp(self): self.width = 4 self.height = 5 self.rect = Rectangle(self.width, self.height) def test_get_rectangle_perimeter(self): in_process = self.rect.get_rectangle_perimeter() in_result = ((self.width + self.height) * 2) self.assertEqual(in_process, in_result) def test_get_rectangle_square(self): in_process = self.rect.get_rectangle_square() in_result = self.width * self.height self.assertEqual(in_process, in_result) def test_get_sum_of_corners(self): in_result = 90 * 3 in_process = self.rect.get_sum_of_corners(3) self.assertEqual(in_process, in_result) def test_get_rectangle_diagonal(self): in_process = self.rect.get_rectangle_diagonal() in_result = sqrt(4**2 + 5**2) self.assertEqual(in_process, in_result) def test_get_radius_of_circumscribed_circle(self): in_process = self.rect.get_radius_of_circumscribed_circle() in_result = (sqrt(4**2 + 5**2)) / 2 self.assertEqual(in_process, in_result) def test_radius_of_inscribed_circle(self): in_process = Rectangle(8, 8).get_radius_of_inscribed_circle() in_result = (8 / 2) self.assertEqual(in_process, in_result) # Errors def test_get_sum_of_corners_ValueError(self): with self.assertRaises(ValueError): self.rect.get_sum_of_corners(5) def test_radius_of_inscribed_circle_invalid(self): with self.assertRaises(ValueError): Rectangle(8, 9).get_radius_of_inscribed_circle()
def test_get_rectangle_diagonal(self): for i in self.arg_list: rect = Rectangle(*i['args']) result3 = rect.get_rectangle_diagonal() self.assertEqual(result3, i['diagonal']) # test rectangle diagonal
class RectangleTestCases(unittest.TestCase): def setUp(self): self.rectangle_value = Rectangle(width=35, height=50) self.square_value = Rectangle(width=35, height=35) def test_get_rectangle_perimeter(self): actual_result = self.rectangle_value.get_rectangle_perimeter() expected_result = (self.rectangle_value.width + self.rectangle_value.height) * 2 self.assertEqual(actual_result, expected_result) def test_get_square_perimeter(self): actual_result = self.square_value.get_rectangle_perimeter() expected_result = (self.square_value.width + self.square_value.height) * 2 self.assertEqual(actual_result, expected_result) def test_get_rectangle_square(self): actual_result = self.rectangle_value.get_rectangle_square() expected_result = self.rectangle_value.width * self.rectangle_value.height self.assertEqual(actual_result, expected_result) def test_get_rectangle_square_for_square(self): actual_result = self.square_value.get_rectangle_square() expected_result = self.square_value.width * self.square_value.height self.assertEqual(actual_result, expected_result) def test_get_sum_of_corners_valid_value(self): for corners in range(1, 4): with self.subTest(i=corners): actual_result = self.rectangle_value.get_sum_of_corners(corners) expected_result = corners * 90 self.assertEqual(actual_result, expected_result) def test_get_sum_of_corners_raises_error(self): with self.assertRaises(ValueError): corners = 5 self.rectangle_value.get_sum_of_corners(corners) def test_get_rectangle_diagonal(self): actual_result = self.rectangle_value.get_rectangle_diagonal() expected_result = math.sqrt(math.pow(self.rectangle_value.height, 2) + math.pow(self.rectangle_value.width, 2)) self.assertEqual(actual_result, expected_result) def test_get_square_diagonal(self): actual_result = self.square_value.get_rectangle_diagonal() expected_result = math.sqrt(math.pow(self.square_value.height, 2) + math.pow(self.square_value.width, 2)) self.assertEqual(actual_result, expected_result) def test_get_radius_of_circumscribed_circle(self): actual_result = self.rectangle_value.get_radius_of_circumscribed_circle() expected_result = (math.sqrt(math.pow(self.rectangle_value.height, 2) + math.pow(self.rectangle_value.width, 2))) / 2 self.assertEqual(actual_result, expected_result) def test_get_radius_of_inscribed_circle_valid_value(self): actual_result = self.square_value.get_radius_of_inscribed_circle() expected_result = self.square_value.width / 2 self.assertEqual(actual_result, expected_result) def test_get_radius_of_inscribed_circle_raises_error(self): with self.assertRaises(ValueError): self.rectangle_value.get_radius_of_inscribed_circle()
class TestRectangle(unittest.TestCase): def setUp(self): self.rect = Rectangle self.normal_rect = Rectangle(2, 4) self.sqr = Rectangle(2, 2) self.invalid_args = ((0, 0), (-1, -1), (-1, 0), (0, -1), (1, 0), (0, 1), (1, -1), (-1, 1)) self.ctrl_rect_diag = math.sqrt( math.pow(self.normal_rect.height, 2) + math.pow(self.normal_rect.width, 2)) self.ctrl_sqr_diag = math.sqrt( math.pow(self.sqr.height, 2) + math.pow(self.sqr.width, 2)) def tearDown(self) -> None: del self.rect def test_invalid_params(self): for args in self.invalid_args: with self.subTest(args=args): self.assertRaises(ValueError, self.rect, *args) def test_unexpected_errors(self): for args in zip(range(1, 8, 2), range(8, 1, -2)): try: self.rect(*args) except Exception as er: self.fail( f'Unexpected error "{type(er)}" happened with args: {args}' ) def test_get_rectangle_perimeter(self): self.assertEqual(self.normal_rect.get_rectangle_perimeter(), 12) def test_get_rectangle_square(self): self.assertEqual(self.normal_rect.get_rectangle_square(), 8) def test_get_sum_of_corners(self): sum_of_corn = self.normal_rect.get_sum_of_corners invalid_num_of_corn = [-1, 0, 5] for val in invalid_num_of_corn: with self.subTest(val=val): self.assertRaises(ValueError, sum_of_corn, val) for val in range(1, 5): with self.subTest(val=val): self.assertEqual(sum_of_corn(val), val * 90) def test_get_rectangle_diagonal(self): inst_rect_diag = self.normal_rect.get_rectangle_diagonal() inst_sqr_diag = self.sqr.get_rectangle_diagonal() self.assertEqual(inst_rect_diag, self.ctrl_rect_diag) self.assertEqual(inst_sqr_diag, self.ctrl_sqr_diag) def test_get_radius_of_circumscribed_circle(self): result_rect = self.normal_rect.get_radius_of_circumscribed_circle() result_sqr = self.sqr.get_radius_of_circumscribed_circle() self.assertEqual(result_rect, self.ctrl_rect_diag / 2) self.assertEqual(result_sqr, self.ctrl_sqr_diag / 2) def test_get_radius_of_inscribed_circle(self): sqr_radius_of_inscr_circ = self.ctrl_sqr_diag / (math.sqrt(2) * 2) self.assertRaises(ValueError, self.normal_rect.get_radius_of_inscribed_circle) self.assertEqual(self.sqr.get_radius_of_inscribed_circle(), sqr_radius_of_inscr_circ)
class RectangleTestCases(unittest.TestCase): def setUp(self): self.rez_list = [{ 'args': (2, 8), 'perimetr': 20, 'square': 16, 'diagonal': math.sqrt(68) }, { 'args': (6, 12), 'perimetr': 36, 'square': 72, 'diagonal': math.sqrt(180) }, { 'args': (5, 5), 'perimetr': 20, 'square': 25, 'diagonal': math.sqrt(50) }] def test_get_rectangle_perimeter(self): for i in self.rez_list: self.example = Rectangle(i['args'][0], i['args'][1]) result = self.example.get_rectangle_perimeter() self.assertEqual(result, i['perimetr']) def test_get_rectangle_square(self): for i in self.rez_list: self.example = Rectangle(i['args'][0], i['args'][1]) result = self.example.get_rectangle_square() self.assertEqual(result, i['square']) def test_get_sum_of_corners(self): for corner in range(1, 5): for i in self.rez_list: self.example = Rectangle(i['args'][0], i['args'][1]) self.assertEqual(self.example.get_sum_of_corners(corner), corner * 90) def test_get_sum_of_corners_invalid_values(self): for corner in range(5, 20): with self.assertRaises(ValueError): for i in self.rez_list: self.example = Rectangle(i['args'][0], i['args'][1]) self.example.get_sum_of_corners(corner) def test_get_rectangle_diagonal(self): for i in self.rez_list: self.example = Rectangle(i['args'][0], i['args'][1]) result = self.example.get_rectangle_diagonal() self.assertEqual(result, i['diagonal']) def test_get_radius_of_circumscribed_circle(self): for i in self.rez_list: self.example = Rectangle(i['args'][0], i['args'][1]) result = self.example.get_radius_of_circumscribed_circle() self.assertEqual(result, i['diagonal'] / 2) def test_get_radius_of_inscribed_circle(self): for i in self.rez_list: if i['args'][0] == i['args'][1]: self.example = Rectangle(i['args'][0], i['args'][1]) result = self.example.get_radius_of_inscribed_circle() self.assertEqual(result, i['diagonal'] / (math.sqrt(2) * 2)) def test_get_radius_of_inscribed_circle_invalid_values(self): with self.assertRaises(ValueError): for i in self.rez_list: if i['args'][0] != i['args'][1]: self.example = Rectangle(i['args'][0], i['args'][1]) self.example.get_radius_of_inscribed_circle()
def test_8_radius_of_inscribed_circle(self): rectangle = Rectangle(width=1, height=1) cheack = rectangle.get_rectangle_diagonal() / 2 * sqrt(2) result = rectangle.get_radius_of_inscribed_circle() self.assertEqual(cheack, result)