class MyTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.calculator = Calculator()

    # Unit test 1
    def test_instantiate_calculator(self):
        self.assertIsInstance(self.calculator, Calculator)

    # Unit test 2
    def test_subtraction(self):
        test_data = CsvReader( BASE_DIR + "/data/UnitTestSubtraction.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(self.calculator.subtract(row['Value 1'], row['Value 2']), result)
            self.assertEqual(self.calculator.result, result)

    # Unit test 3
    def test_addition(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestAddition.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)

    # Unit test 4
    def test_multiplication(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestMultiplication.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(self.calculator.multiply(row['Value 1'], row['Value 2']), result)
            self.assertEqual(self.calculator.result, result)

    # Unit test 5
    def test_division(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestDivision.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(round(self.calculator.divide(row['Value 1'], row['Value 2'])), round(result))
            self.assertEqual(round(self.calculator.result), round(result))

    # Unit test 6
    def test_square(self):
        test_data = CsvReader(BASE_DIR + "/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.result, result)

    # Unit test 7
    def test_square_root(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestSquareRoot.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(round(self.calculator.squareroot(row['Value 1'])), round(result))
            self.assertEqual(round(self.calculator.result), round(result))
Beispiel #2
0
    def test_module(self):
        """
        Tests all calculator modules work together nicely and the correct
        module is called for the given calculation operation.
        """
        calculator = Calculator(Adder(), Subtracter(), Multiplier(), Divider(),
                                Squarer())

        calculator.enter_number(2)
        calculator.square()  # result = 4

        calculator.enter_number(3)
        calculator.multiply()  # result = 12

        calculator.enter_number(8)
        calculator.add()  # result = 20

        calculator.enter_number(4)
        calculator.divide()  # result = 5

        calculator.enter_number(6)
        result = calculator.subtract()  # result = -1

        self.assertEqual(-1, result)
class CalculatorTests(TestCase):
    """ A Calculator of Squarer test cases """
    def setUp(self):
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()
        self.squarer = Squarer()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider,
                                     self.squarer)

    def test_insufficient_operands(self):
        """
        Tests all calculator operations throws InsufficientOperands exceptions
        if the inadequate amount of operands are provided.
        """
        with self.assertRaises(InsufficientOperands):
            self.calculator.square()

        self.calculator.enter_number(2)

        with self.assertRaises(InsufficientOperands):
            self.calculator.add()

        with self.assertRaises(InsufficientOperands):
            self.calculator.subtract()

        with self.assertRaises(InsufficientOperands):
            self.calculator.multiply()

        with self.assertRaises(InsufficientOperands):
            self.calculator.divide()

    def test_adder_call(self):
        """
        Tests that Add calls the adder's calc function
        """
        self.adder.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.add()

        self.adder.calc.assert_called_with(1, 2)

    def test_subtracter_call(self):
        """
        Tests that Subtract calls the subtracters's calc function
        """
        self.subtracter.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.subtract()

        self.subtracter.calc.assert_called_with(1, 2)

    def test_multiplier_call(self):
        """
        Tests that Multiply calls the multiplier's calc function
        """
        self.multiplier.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.multiply()

        self.multiplier.calc.assert_called_with(1, 2)

    def test_divider_call(self):
        """
        Tests that Divide calls the Divider's calc function
        """
        self.divider.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.divide()

        self.divider.calc.assert_called_with(1, 2)

    def test_squarer_call(self):
        """
        Tests that Square calls the Squarer's calc function
        """
        self.squarer.calc = MagicMock(return_value=0)

        self.calculator.enter_number(2)
        self.calculator.square()

        self.squarer.calc.assert_called_with(2)