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 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_subtract(): calculator = Calculator() assert calculator.subtract(5, 1) == 4 assert calculator.subtract(3, 2) == 1
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, [])
def test_subtract_method_calculator(self): calculator = Calculator() self.assertEqual(calculator.subtract(2, 2), 0) self.assertEqual(calculator.result, 0)