示例#1
0
class CalculatorTest(unittest.TestCase):
    """unitetestモジュールの簡単な実行例その1。"""
    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self._calculator = Calculator()

    def tearDown(self):
        pass

    def test_add(self):
        self.assertEqual(self._calculator.add(1.5, 1.5), 3)

    def test_subtract(self):
        self.assertEqual(self._calculator.subtract(1.5, 1), 0.5)

    def test_multiply(self):
        self.assertEqual(self._calculator.multiply(1.5, 1.5), 2.25)

    def test_divide(self):
        self.assertEqual(self._calculator.divide(1.5, 0.3), 5)
class CalculatorTestCase(unittest.TestCase):

    def setUp(self):
        self.cal = Calculator()

    def testAddition(self):
        result = self.cal.addition(0.001, 0.003)
        self.assertEqual(0.004, result)

    def testSubtraction(self):
        result = self.cal.subtraction(0.1, 0.001)
        self.assertEqual(0.099, result)

    def testMultiply(self):
        result = self.cal.multiply(0.001, 10)
        self.assertEquals(0.01, result)

    def testDivide(self):
        result = self.cal.divide(5, 1000)
        self.assertEquals(0.005, result)

    def testDivideError(self):
        self.assertRaises(ValueError, self.cal.divide, 10, 0)

    def testLogMyMore(self):
        result = self.cal.logMy(25, 5)
        self.assertEquals(2, result)

    def testLogMyLess(self):
        result = self.cal.logMy(0.125, 2)
        self.assertEquals(-3, result)

    def testLog(self):
        log1 = self.cal.logMath(0.221, 2.5)
        log2 = self.cal.logMy(0.221, 2.5)
        self.assertEquals(round(log1, 5), log2)

    def testLogMyErrorOne(self):
        self.assertRaises(ValueError, self.cal.logMy, 100, 0)

    def testLogMyErrorTwo(self):
        self.assertRaises(ZeroDivisionError, self.cal.logMy, 100, 1)

    def testLogMathMore(self):
        result = self.cal.logMath(25, 5)
        self.assertEquals(2, result)

    def testLogMathLess(self):
        result = self.cal.logMath(0.125, 2)
        self.assertEquals(-3, result)

    def testLogMathErrorOne(self):
        self.assertRaises(ValueError, self.cal.logMath, 100, 0)

    def testLogMathErrorTwo(self):
        self.assertRaises(ZeroDivisionError, self.cal.logMath, 100, 1)
class TestCalculator(unittest.TestCase):
    # this is the Calculator class instance.
    calculator = None

    # class level setup function, execute once only before any test function.
    @classmethod
    def setUpClass(cls):
        load_test_data()
        print('')
        print('setUpClass')

    # class level setup function, execute once only after all test function's execution.
    @classmethod
    def tearDownClass(cls):
        close_test_data_file()
        print('')
        print('tearDownClass')

    # execute before every test case function run.
    def setUp(self):
        self.calculator = Calculator()
        print('')
        print('setUp')

    # execute after every test case function run.
    def tearDown(self):
        # release the Calculator object.
        if self.calculator is not None:
            self.calculator = None
        print('')
        print('tearDown')

    # below are function that test Calculator class's plus, minus, multiple and divide functions.
    def test_addition(self):
        print('')
        print('******test_addition******')
        # get each row text from the csv file.
        for row in test_data_row_list[0]:
            # the first column in the text line is x value.
            x = row[0]
            # the second column in the text line is y value.
            y = row[1]
            # the third column in the text line is (x + y) value.
            expect_result = row[2]
            result = self.calculator.sum(x, y)

            print(
                str(x) + ' + ' + str(y) + ' = ' + str(result) + ', expect ' +
                str(expect_result))
            self.assertEqual(float(result), float(expect_result))

    def test_subtraction(self):
        print('')
        print('******test_subtraction******')
        for row in test_data_row_list[1]:
            x = row[0]
            y = row[1]
            expect_result = row[2]
            result = self.calculator.subtract(x, y)

            print(
                str(x) + ' - ' + str(y) + ' = ' + str(result) + ', expect ' +
                str(expect_result))
            self.assertAlmostEqual(float(result), float(expect_result))

    def test_multiplication(self):
        print('')
        print('******test_multiplication******')
        for row in test_data_row_list[2]:
            x = row[0]
            y = row[1]
            # the fifth column in the text line is (x * y) value.
            expect_result = row[2]
            result = self.calculator.multiply(x, y)

            print(
                str(x) + ' * ' + str(y) + ' = ' + str(result) + ', expect ' +
                str(expect_result))
            self.assertEqual(float(result), float(expect_result))

    def test_division(self):
        print('')
        print('******test_division******')
        for row in test_data_row_list[3]:
            x = row[0]
            y = row[1]
            # the sixth column in the text line is (x / y) value.
            expect_result = row[2]
            result = self.calculator.divide(x, y)

            print(
                str(x) + ' / ' + str(y) + ' = ' + str(result) + ', expect ' +
                str(expect_result))
            self.assertEqual(float(result), float(expect_result))

    def test_squareRoot(self):
        print('')
        print('******test_squareRoot******')
        for row in test_data_row_list[4]:
            x = row[0]
            expect_result = row[1]
            result = self.calculator.root(x, 2)

            print(' √ ' + str(x) + ' = ' + str(result) + ', expect ' +
                  str(expect_result))
            self.assertEqual(float(result), float(expect_result))

    def test_squared(self):
        print('')
        print('******test_squared******')
        for row in test_data_row_list[5]:
            x = row[0]
            # the sixth column in the text line is (x / y) value.
            expect_result = row[1]
            result = self.calculator.power(x, 2)

            print(
                str(x) + '² ' + ' = ' + str(result) + ', expect ' +
                str(expect_result))
            self.assertEqual(float(result), float(expect_result))
def test_multiply():
    calculator = Calculator()
    assert calculator.multiply(2, 2) == 4
    assert calculator.multiply(5, 6) == 30
class TestCalculator(unittest.TestCase):
    calculator = None

    @classmethod
    def setUpClass(cls):
        print('')
        print('setUpClass')

    @classmethod
    def tearDownClass(cls):
        print('')
        print('tearDownClass')

    def setUp(self):
        self.calculator = Calculator()
        print('')
        print('setUp')

    # execute after every tests case function run.
    def tearDown(self):
        # release the Calculator object.
        if self.calculator is not None:
            self.calculator = None
        print('')
        print('tearDown')

    # below are function that tests Calculator class's plus, minus, multiple and divide functions.
    def testInstantiateCalculator(self):
        self.assertIsInstance(self.calculator, Calculator)

    def testAddition(self):
        rows = readCSV("tests/test_cases/Addition.csv")
        for row in rows:
            result = float(row["Result"])
            x = float(row["Value 1"])
            y = float(row["Value 2"])
            self.assertEqual(self.calculator.add(x, y), result)

    def testSubtraction(self):
        rows = readCSV("tests/test_cases/Subtraction.csv")
        for row in rows:
            result = float(row["Result"])
            y = float(row["Value 1"])
            x = float(row["Value 2"])
            self.assertEqual(self.calculator.subtract(x, y), result)

    def testDivision(self):
        self.assertRaises(ZeroDivisionError, self.calculator.divide, 1, 0)
        rows = readCSV("tests/test_cases/Division.csv")
        for row in rows:
            result = float(row["Result"])
            y = float(row["Value 1"])
            x = float(row["Value 2"])
            self.assertAlmostEqual(self.calculator.divide(x, y), result)

    def testMultiplication(self):
        rows = readCSV("tests/test_cases/Multiplication.csv")
        for row in rows:
            result = float(row["Result"])
            x = float(row["Value 1"])
            y = float(row["Value 2"])
            self.assertAlmostEqual(self.calculator.multiply(x, y), result)

    def testSquare(self):
        rows = readCSV("tests/test_cases/Square.csv")
        for row in rows:
            result = float(row["Result"])
            x = float(row["Value 1"])
            self.assertAlmostEqual(self.calculator.square(x), result)

    def testSquareRoot(self):
        rows = readCSV("tests/test_cases/SquareRoot.csv")
        for row in rows:
            result = float(row["Result"])
            x = float(row["Value 1"])
            self.assertAlmostEqual(self.calculator.squareRoot(x), result)

    def testResultProperty(self):
        self.calculator.results.clear()
        self.assertEqual(self.calculator.results, [])
示例#6
0
 def test_multiply_method_calculator(self):
     calculator = Calculator()
     self.assertEqual(calculator.multiply(2, 2), 4)
     self.assertEqual(calculator.result, 4)