示例#1
0
class MyTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.calculator = Calculator()

    def test_instantiate_calculator(self):
        self.assertIsInstance(self.calculator, Calculator)

    def test_add_method_calculator(self):
        test_data = CsvReader("Tests/Data/addition.csv").data
        for row in test_data:
            self.assertEqual(
                self.calculator.add(int(row['Value 1']), int(row['Value 2'])),
                int(row['Result']))
            self.assertEqual(self.calculator.result, int(row['Result']))

    def test_subtract_method_calculator(self):
        test_data = CsvReader("Tests/Data/subtraction.csv").data
        for row in test_data:
            self.assertEqual(
                self.calculator.subtract(int(row['Value 2']),
                                         int(row['Value 1'])),
                int(row['Result']))
            self.assertEqual(self.calculator.result, int(row['Result']))

    def test_multiply_method_calculator(self):
        test_data = CsvReader("Tests/Data/multiplication.csv").data
        for row in test_data:
            self.assertEqual(
                self.calculator.multiply(int(row['Value 1']),
                                         int(row['Value 2'])),
                int(row['Result']))
            self.assertEqual(self.calculator.result, int(row['Result']))

    def test_divide_method_calculator(self):
        test_data = CsvReader("Tests/Data/division.csv").data
        for row in test_data:
            x = float(row['Result'])
            self.assertEqual(
                self.calculator.divide(int(row['Value 2']),
                                       int(row['Value 1'])), round(x, 7))
            self.assertEqual(self.calculator.result, round(x, 7))

    def test_square_method_calculator(self):
        test_data = CsvReader("Tests/Data/square.csv").data
        for row in test_data:
            self.assertEqual(self.calculator.squaring(int(row['Value 1'])),
                             int(row['Result']))
            self.assertEqual(self.calculator.result, int(row['Result']))

    def test_square_rt_method_calculator(self):
        test_data = CsvReader("Tests/Data/square_root.csv").data
        for row in test_data:
            y = float(row['Result'])
            self.assertEqual(self.calculator.square_rt(int(row['Value 1'])),
                             round(y, 7))
            self.assertEqual(self.calculator.result, round(y, 7))

    if __name__ == '__main__':
        unittest.main()
示例#2
0
class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.calculator = Calculator()

    def test_instantiate_calculator(self):
        self.assertIsInstance(self.calculator, Calculator)

    def test_cal_return_sum(self):
        result = self.calculator.add(1, 2)
        self.assertEqual(3, result)

    def test_cal_return_diff(self):
        result = self.calculator.subtract(1, 2)
        self.assertEqual(-1, result)

    def test_cal_return_mul(self):
        result = self.calculator.multiply(1, 2)
        self.assertEqual(2, result)

    def test_cal_return_div(self):
        result = self.calculator.divide(2, 1)
        self.assertEqual(2, result)

    def test_cal_access_sum_result(self):
        self.calculator.add(1, 2)
        self.assertEqual(3, self.calculator.result)

    def test_cal_access_diff_result(self):
        self.calculator.subtract(1, 2)
        self.assertEqual(-1, self.calculator.result)

    def test_cal_access_multi_result(self):
        self.calculator.multiply(1, 2)
        self.assertEqual(2, self.calculator.result)

    def test_cal_access_div_result(self):
        self.calculator.divide(2, 1)
        self.assertEqual(2, self.calculator.result)

    def test_multiple_cals(self):
        cal1 = Calculator()
        cal2 = Calculator()
        self.calculator.add(cal1.add(1, 2), cal2.subtract(3, 4))
        self.assertEqual(2, self.calculator.result)
class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.calculator = Calculator()

    def test_instantiate_calculator(self):
        self.assertIsInstance(self.calculator, Calculator)

    def test_calculator_return_sum(self):
        result = self.calculator.add(1, 2)
        self.assertEqual(3, result)

    def test_calculator_access_sum_result(self):
        self.calculator.add(1, 2)
        self.assertEqual(3, self.calculator.result)

    def test_calculator_return_difference(self):
        result = self.calculator.subtract(2, 1)
        self.assertEqual(-1, result)

    def test_calculator_access_difference_result(self):
        self.calculator.subtract(2, 1)
        self.assertEqual(-1, self.calculator.result)

    def test_calculator_return_product(self):
        result = self.calculator.multiply(1, 2)
        self.assertEqual(2, result)

    def test_calculator_access_product_result(self):
        self.calculator.multiply(1, 2)
        self.assertEqual(2, self.calculator.result)

    def test_calculator_divide(self):
        result = self.calculator.divide(5, 10)
        self.assertEqual(2, result)

    def test_calculator_root(self):
        result = self.calculator.squareroot(9)
        self.assertEqual(3, result)

    def test_calculator_power(self):
        result = self.calculator.square(4)
        self.assertEqual(16, result)
示例#4
0
class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.calculator = Calculator()

    def test_instantiate_calculator(self):
        self.assertIsInstance(self.calculator, Calculator)

    def test_cal_return_sum(self):
        result = self.calculator.add(1, 2)
        self.assertEqual(3, result)

    def test_cal_return_diff(self):
        result = self.calculator.subtract(1, 2)
        self.assertEqual(-1, result)

    def test_cal_return_mul(self):
        result = self.calculator.multiply(1, 2)
        self.assertEqual(2, result)

    def test_cal_return_div(self):
        result = self.calculator.divide(2, 1)
        self.assertEqual(2, result)

    def test_cal_return_expo(self):
        result = self.calculator.exponent(2, 3)
        self.assertEqual(8, result)

    def test_cal_return_nroot(self):
        result = self.calculator.nroot(3, 27)
        self.assertEqual(3, result)

    def test_cal_return_log(self):
        result = self.calculator.log(10, 10)
        self.assertEqual(1, result)

    def test_cal_access_sum_result(self):
        self.calculator.add(1, 2)
        self.assertEqual(3, self.calculator.result)

    def test_cal_access_diff_result(self):
        self.calculator.subtract(1, 2)
        self.assertEqual(-1, self.calculator.result)

    def test_cal_access_mul_result(self):
        self.calculator.multiply(1, 2)
        self.assertEqual(2, self.calculator.result)

    def test_cal_access_div_result(self):
        self.calculator.divide(2, 1)
        self.assertEqual(2, self.calculator.result)

    def test_cal_access_exp_result(self):
        self.calculator.exponent(2, 3)
        self.assertEqual(8, self.calculator.result)

    def test_cal_access_nroot_result(self):
        self.calculator.nroot(3, 27)
        self.assertEqual(3, self.calculator.result)

    def test_cal_access_log_result(self):
        self.calculator.log(10, 10)
        self.assertEqual(1, self.calculator.result)

    def test_multiple_cal(self):
        cal1 = Calculator()
        cal2 = Calculator()
        self.calculator.add(cal1.add(1, 2), cal2.subtract(3, 4))
        self.assertEqual(2, self.calculator.result)
示例#5
0
def test_multiply():
    c = Calculator(1000)
    with pytest.raises(ValueError):
        c.add('test text')
    assert c.multiply(5) == 5000