def main(operation_code, num1, num2): calculator = Calculator() try: result = calculator.execute_operation(operation_code, num1, num2) print(f'Result is: {result}') except Exception as e: print(e)
def test_square_root_method_calculator(self): calculator = Calculator() test_data = CsvReader('/data/squareRoot.csv').data for row in test_data: self.assertEqual(calculator.square_root(float(row['Value 1'])), round(float(row['Result']), 8)) self.assertEqual(calculator.result, round(float(row['Result']), 8)) test_data.clear()
def test_calculator_soma(self): with patch('src.calculator.calculator.soma', MagicMock()) as mocked_operation: # it will fail since self.calculator was instantiated outside scope # self.calculator.execute_operation(0, self.num1, self.num2) calculator = Calculator() calculator.execute_operation(0, self.num1, self.num2) assert mocked_operation.mock_calls == [call(1, 2)]
def test_add_method_calculator(self): calculator = Calculator() test_data = CsvReader('/data/addition.csv').data for row in test_data: self.assertEqual( calculator.add(float(row['Value 1']), float(row['Value 2'])), float(row['Result'])) self.assertEqual(calculator.result, float(row['Result'])) test_data.clear()
class TestCalculator(TestCase): calculator = None #useless: just to demonstrate def setUp(self): if self.calculator is None: #useless: just to demonstrate self.calculator = Calculator() self.num1 = 1 self.num2 = 2 def test_validate_params(self): assert self.calculator.validate_numbers(1, 2) def test_validate_params_error(self): try: self.calculator.validate_numbers('a', 2) except Exception as e: assert isinstance(e, TypeError) assert str(e) == 'Error: Operands must be numbers' def test_calculator_soma(self): with patch('src.calculator.calculator.soma', MagicMock()) as mocked_operation: # it will fail since self.calculator was instantiated outside scope # self.calculator.execute_operation(0, self.num1, self.num2) calculator = Calculator() calculator.execute_operation(0, self.num1, self.num2) assert mocked_operation.mock_calls == [call(1, 2)] def test_calculator_subtract(self): with patch('src.calculator.calculator.subtract', MagicMock()) as mocked_operation: calculator = Calculator() calculator.execute_operation(1, self.num1, self.num2) assert mocked_operation.mock_calls == [call(1, 2)] def test_calculator_multiply(self): with patch('src.calculator.calculator.multiply', MagicMock()) as mocked_operation: calculator = Calculator() calculator.execute_operation(2, self.num1, self.num2) assert mocked_operation.mock_calls == [call(1, 2)] def test_calculator_divide(self): with patch('src.calculator.calculator.divide', MagicMock()) as mocked_operation: calculator = Calculator() calculator.execute_operation(3, self.num1, self.num2) assert mocked_operation.mock_calls == [call(1, 2)] def tearDown(self): self.num1 = 0 self.num2 = 0
class CalculatorTestCase(unittest.TestCase): def setUp(self) -> None: self.calculator = Calculator() def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator) def test_addition(self): test_data = CsvReader("../tests/Data/Unit Test Addition.csv").data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.add(row['Value 1'], row['Value 2']), result) self.assertEqual(self.calculator.result, result) def test_subtraction(self): test_data = CsvReader("../tests/Data/Unit Test Subtraction.csv").data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.sub(row['Value 1'], row['Value 2']), result) self.assertEqual(self.calculator.result, result) def test_multiplication(self): test_data = CsvReader( "../tests/Data/Unit Test Multiplication.csv").data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.mult(row['Value 1'], row['Value 2']), result) def test_division(self): test_data = CsvReader("../tests/Data/Unit Test Division.csv").data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.divi(row['Value 1'], row['Value 2']), result) self.assertEqual(self.calculator.result, result) def test_square(self): test_data = CsvReader("../tests/Data/Unit Test Square.csv").data for row in test_data: result = float(row['Result']) self.assertEqual(self.calculator.squ(row['Value 1']), result) def test_square_root(self): test_data = CsvReader("../tests/Data/Unit Test Square Root.csv").data for row in test_data: result = round(float(row['Result']), 8) self.assertEqual(self.calculator.root(row['Value 1']), result) self.assertEqual(self.calculator.result, result) def test_divide_zero(self): self.assertRaises(ZeroDivisionError, self.calculator.divi(0, 0)) def test_result_is_zero_calculator(self): self.assertEqual(self.calculator.result, 0)
class CalculatorTestCase(unittest.TestCase): def setUp(self) -> None: self.calculator = Calculator() def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator) def test_result_is_zero_calculator(self): self.assertEqual(self.calculator.result, 0) def test_addition(self): file = Reader("testCases/Addition.csv").content print("TEST ADDITION\n") for row in file: result = float(row[2]) self.assertEqual(self.calculator.add(row[0], row[1]), float(row[2])) self.assertEqual(self.calculator.result, result) def test_subtraction(self): file = Reader("testCases/Subtraction.csv").content print("TESTING SUBTRACTION\n") for row in file: self.assertEqual(self.calculator.subtract(row[0], row[1]), float(row[2])) def test_multiplication(self): file = Reader("testCases/Multiplication.csv").content print("TESTING MULTIPLICATION\n") for row in file: result = float(row[2]) self.assertEqual(self.calculator.multiply(row[0], row[1]), float(row[2])) def test_division(self): file = Reader("testCases/Division.csv").content print("TEST DIVISION\n") for row in file: result = float(row[2]) self.assertEqual(self.calculator.divide(row[0], row[1]), float(row[2])) def test_square(self): file = Reader("testCases/Square.csv").content print("TEST SQUARE\n") for row in file: result = float(row[1]) self.assertEqual(self.calculator.square(row[0]), float(row[1])) self.assertEqual(self.calculator.result, result) def test_square_root(self): file = Reader("testCases/SquareRoot.csv").content print("TEST SQUARE ROOT\n") for row in file: result = float(row[1]) self.assertAlmostEqual(self.calculator.square_root(row[0]), float(row[1])) self.assertAlmostEqual(self.calculator.result, result)
class CalculatorTestCase(unittest.TestCase): def setUp(self) -> None: self.calculator = Calculator() def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator) def test_result_is_zero_calculator(self): self.assertEqual(self.calculator.result, 0) def test_Addition(self): file = Reader("testCases/Unit_Test_Addition.csv").content print("test addition \n") for row in file: result = float(row[2]) self.assertEqual(self.calculator.add(row[0], row[1]), float(row[2])) self.assertEqual(self.calculator.result, result) def test_Subtraction(self): file = Reader("testCases/Unit_Test_Subtraction.csv").content print("test subtraction \n") for row in file: result = float(row[2]) self.assertEqual(self.calculator.subtract(row[0], row[1]), float(row[2])) self.assertEqual(self.calculator.result, result) def test_Division(self): file = Reader("testCases/Unit_Test_Division.csv").content print("test division \n") for row in file: result = float(row[2]) self.assertEqual(self.calculator.division(row[0], row[1]), float(row[2])) self.assertEqual(self.calculator.result, result) def test_Multiplication(self): file = Reader("testCases/Unit_Test_Multiplication.csv").content print("test multiplication \n") for row in file: result = float(row[2]) self.assertEqual(self.calculator.multiply(row[0], row[1]), float(row[2])) self.assertEqual(self.calculator.result, result) def test_Square(self): file = Reader("testCases/Unit_Test_Square.csv").content print("test square \n") for row in file: result = float(row[1]) self.assertEqual(self.calculator.square(row[0]), float(row[1])) self.assertEqual(self.calculator.result, result) def test_Square_Root(self): file = Reader("testCases/Unit_Test_Square_Root.csv").content print("test square root \n") for row in file: result = float(row[1]) self.assertAlmostEqual(self.calculator.squareRoot(row[0]), float(row[1])) self.assertAlmostEqual(self.calculator.result, result)
def test_square_root_method_calculator(self): calculator = Calculator() self.assertEqual(calculator.square_root(4), 2) self.assertEqual(calculator.result, 2)
def test_square_method_calculator(self): calculator = Calculator() self.assertEqual(calculator.square(2), 4) self.assertEqual(calculator.result, 4)
def test_divide_method_calculator(self): calculator = Calculator() self.assertEqual(calculator.divide(2, 2), 1) self.assertEqual(calculator.result, 1)
def test_multiply_method_calculator(self): calculator = Calculator() self.assertEqual(calculator.multiply(2, 2), 4) self.assertEqual(calculator.result, 4)
def test_subtract_method_calculator(self): calculator = Calculator() self.assertEqual(calculator.subtract(2, 2), 0) self.assertEqual(calculator.result, 0)
def test_add_method_calculator(self): calculator = Calculator() self.assertEqual(calculator.add(2, 2), 4) self.assertEqual(calculator.result, 4)
def test_multiplication(self): mult = Calculator.multiply(1, 2) self.assertIsInstance(mult, Calculator)
def test_addition(self): add = Calculator.add(1, 2) self.assertIsInstance(add, Calculator)
def test_calculator_divide(self): with patch('src.calculator.calculator.divide', MagicMock()) as mocked_operation: calculator = Calculator() calculator.execute_operation(3, self.num1, self.num2) assert mocked_operation.mock_calls == [call(1, 2)]
def test_calculator_multiply(self): with patch('src.calculator.calculator.multiply', MagicMock()) as mocked_operation: calculator = Calculator() calculator.execute_operation(2, self.num1, self.num2) assert mocked_operation.mock_calls == [call(1, 2)]
class CalculatorTestCase(unittest.TestCase): def setUp(self) -> None: self.calculator = Calculator() def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator) def test_result_is_zero_calculator(self): self.assertEqual(self.calculator.result, 0) def test_addition(self): file = Reader("tests/data/Unit_Test_Addition.csv").content print("TESTING ADDITION\n") for row in file: result = float(row[2]) # print('---->: {0:4} + {1:4} = {2:4}'.format(row[0], row[1], row[2])) self.assertEqual(self.calculator.add(row[0], row[1]), float(row[2])) self.assertEqual(self.calculator.result, result) # self.assertEqual(self.calculator.add(5, 6), 11) def test_subtraction(self): file = Reader("tests/data/Unit_Test_Subtraction.csv").content print("TESTING SUBTRACTION\n") for row in file: # print('---->: {0:4} - {1:4} = {2:4}'.format(row[1], row[0], row[2])) self.assertEqual(self.calculator.subtract(row[0], row[1]), float(row[2])) # self.assertEqual(self.calculator.subtract(5, 6), 1) def test_multiplication(self): file = Reader("tests/data/Unit_Test_Multiplication.csv").content print("TESTING MULTIPLICATION\n") for row in file: result = float(row[2]) # print('---->: {0:4} x {1:4} = {2:4}'.format(row[0], row[1], row[2])) self.assertEqual(self.calculator.multiply(row[0], row[1]), float(row[2])) # self.assertEqual(self.calculator.multiply(5, 6), 30) def test_division(self): file = Reader("tests/data/Unit_Test_Division.csv").content print("TESTING DIVISION\n") for row in file: result = float(row[2]) # print('---->: {0:4} / {1:4} = {2:4}'.format(row[1], row[0], row[2])) self.assertEqual(self.calculator.divide(row[0], row[1]), float(row[2])) # self.assertEqual(self.calculator.divide(6, 30), 5) def test_square(self): file = Reader("tests/data/Unit_Test_Square.csv").content print("TESTING SQUARE\n") for row in file: result = float(row[1]) # print('---->: {0:4} ^ {1:2} = {2:4}'.format(row[0], 2, row[1])) self.assertEqual(self.calculator.square(row[0]), float(row[1])) self.assertEqual(self.calculator.result, result) # self.assertEqual(self.calculator.square(5), 25) def test_square_root(self): file = Reader("tests/data/Unit_Test_Square_Root.csv").content print("TESTING SQUARE ROOT\n") for row in file: result = float(row[1]) # print('---->: Square Root of {0:4} = {1:4}'.format(row[0], row[1])) self.assertAlmostEqual(self.calculator.square_root(row[0]), float(row[1])) self.assertAlmostEqual(self.calculator.result, result)
class CalculatorTestCase(unittest2.TestCase): """unit tests for calculator functions.""" def setUp(self) -> None: self.calculator = Calculator() def test_results_property_calculator(self): self.assertEqual(self.calculator.get_result, 0) def test_add_method_calculator(self): self.assertEqual(self.calculator.add(2, 2), 4) self.assertEqual(self.calculator.get_result, 4) test_data = CsvReader('data/UnitTestAddition.csv').data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.add(row['Value 2'], row['Value 1']), result) self.assertEqual(self.calculator.get_result, result) def test_subtract_method_calculator(self): self.assertEqual(self.calculator.subtract(2, 2), 0) self.assertEqual(self.calculator.get_result, 0) test_data = CsvReader('data/UnitTestSubtraction.csv').data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.subtract(row['Value 2'], row['Value 1']), result) self.assertEqual(self.calculator.get_result, result) def test_multiply_method_calculator(self): self.assertEqual(self.calculator.multiply(2, 2), 4) self.assertEqual(self.calculator.get_result, 4) test_data = CsvReader('data/UnitTestMultiplication.csv').data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.multiply(row['Value 2'], row['Value 1']), result) self.assertEqual(self.calculator.get_result, result) def test_divide_method_calculator(self): self.assertEqual(self.calculator.divide(2, 2), 1) self.assertEqual(self.calculator.get_result, 1) test_data = CsvReader('data/UnitTestDivision.csv').data for row in test_data: result = float(row['Result']) self.assertEqual( self.calculator.divide(row['Value 2'], row['Value 1']), result) self.assertEqual(self.calculator.get_result, result) def test_square_method_calculator(self): self.assertEqual(self.calculator.square(2), 4) self.assertEqual(self.calculator.get_result, 4) test_data = CsvReader('data/UnitTestSquare.csv').data for row in test_data: result = float(row['Result']) self.assertEqual(self.calculator.square(row['Value 1']), result) self.assertEqual(self.calculator.get_result, result) def test_square_root_method_calculator(self): self.assertEqual(self.calculator.square_root(4), 2) self.assertEqual(self.calculator.get_result, 2) test_data = CsvReader('data/UnitTestSquareRoot.csv').data for row in test_data: result = float(row['Result']) self.assertEqual(self.calculator.square_root(row['Value 1']), result) self.assertEqual(self.calculator.get_result, result) def test_instantiate_calculator(self): self.assertIsInstance(self.calculator, Calculator)
def test_subtraction(self): sub = Calculator.subtract(1, 2) self.assertIsInstance(sub, Calculator)
def calculate(a, b): # Use a breakpoint in the code line below to debug your script. calc = Calculator() print('done') # Press Ctrl+F8 to toggle the breakpoint.
def setUp(self): if self.calculator is None: #useless: just to demonstrate self.calculator = Calculator() self.num1 = 1 self.num2 = 2
def setUp(self) -> None: self.calculator = Calculator()
def test_division(self): div = Calculator.divide(1, 2) self.assertIsInstance(div, Calculator)