class MyTestCase(unittest.TestCase): def setUp(self) -> None: self.calculator = Calculator() def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator) def test_addition(self): add = self.calculator.add(4 ,6) self.assertEqual(add, 10) def test_subtraction(self): subtract = self.calculator.subtract(6 ,3) self.assertEqual(subtract, 3) def test_division(self): divide = self.calculator.divide(10, 2) self.assertEqual(divide, 5) def test_multiplication(self): product = self.calculator.multiply(3, 3) self.assertEqual(product, 9) def test_power(self): power = self.calculator.square(3, 2) self.assertEqual(power, 9) def test_root(self): root = self.calculator.root(9, 2) self.assertEqual(root, 3) def test_results_property(self): self.assertEqual(self.calculator.result, 0)
class MyTestCase(unittest.TestCase): def setUp(self) -> None: self.calculator = Calculator() def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator) def test_addition(self): add = self.calculator.add(2, 2) self.assertEqual(add, 4) def test_subtraction(self): subtract = self.calculator.subtract(2, 2) self.assertEqual(subtract, 0) def test_multiplication(self): product = self.calculator.product(3, 2) self.assertEqual(product, 6) def test_division(self): divide = self.calculator.divide(2, 1) self.assertEqual(divide, 2) def test_power(self): power = self.calculator.power(3, 2) self.assertEqual(power, 9) def test_root(self): root = self.calculator.root(16, 2) self.assertEqual(root, 4)
def square_root_test(self): calculator = Calculator() result = calculator.root(8) self.assertEqual(3, result)
class MyTestCase(unittest.TestCase): def setUp(self): self.calculator = Calculator() # To test instantiation of calculator class def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator) # Testing addition def test_addition(self): test_data = CSVReader( '/Tests/Data_Calculator/Unit Test Addition.csv').data for row in test_data: self.assertEqual( self.calculator.add(row['Value 1'], row['Value 2']), int(row['Result'])) self.assertEqual(self.calculator.result, int(row['Result'])) test_data.clear() # Testing subtraction def test_subtraction(self): test_data = CSVReader( '/Tests/Data_Calculator/Unit Test Subtraction.csv').data for row in test_data: self.assertEqual( self.calculator.subtract(row['Value 1'], row['Value 2']), int(row['Result'])) self.assertEqual(self.calculator.result, int(row['Result'])) test_data.clear() # Testing multiplication def test_multiplication(self): test_data = CSVReader( '/Tests/Data_Calculator/Unit Test Multiplication.csv').data for row in test_data: self.assertEqual( self.calculator.multiply(row['Value 1'], row['Value 2']), int(row['Result'])) self.assertEqual(self.calculator.result, int(row['Result'])) test_data.clear() # Testing division def test_division(self): test_data = CSVReader( '/Tests/Data_Calculator/Unit Test Division.csv').data for row in test_data: self.assertAlmostEqual( float(row['Result']), self.calculator.divide(row['Value 1'], row['Value 2'])) self.assertAlmostEqual(float(row['Result']), round(self.calculator.result, 9)) test_data.clear() # Testing squared def test_square(self): test_data = CSVReader( '/Tests/Data_Calculator/Unit Test Square.csv').data for row in test_data: self.assertEqual(self.calculator.squared(row['Value 1']), int(row['Result'])) self.assertEqual(self.calculator.result, int(row['Result'])) test_data.clear() # Testing squared root def test_root(self): test_data = CSVReader( '/Tests/Data_Calculator/Unit Test Square Root.csv').data for row in test_data: self.assertAlmostEqual(self.calculator.root(row['Value 1']), round(float(row['Result']), 9)) self.assertAlmostEqual(round(float(row['Result']), 9), round(self.calculator.result, 9)) test_data.clear() def test_results(self): self.assertEqual(self.calculator.result, 0)