class CalculatorTest(unittest.TestCase): def setUp(self): self.calc = Calculator() def test_last_answer_init(self): value = self.calc.last_answer self.assertEqual(value, 0.0, FAILURE) def test_add(self): value = self.calc.add(NUMBER_1, NUMBER_2) self.assertEqual(value, 6.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_subtract(self): value = self.calc.subtract(NUMBER_1, NUMBER_2) self.assertEqual(value, 1.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_subtract_negative(self): value = self.calc.subtract(NUMBER_2, NUMBER_1) self.assertEqual(value, -1.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_multiply(self): value = self.calc.multiply(NUMBER_1, NUMBER_2) self.assertEqual(value, 6.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_divide(self): value = self.calc.divide(NUMBER_1, NUMBER_2) self.assertEqual(value, 1.5, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_divide_by_zero(self): self.assertRaises(ZeroDivisionError, self.calc.divide, NUMBER_1, 0)
def test_div(): calc = Calculator() assert calc.divide(5, 5) == 1 assert calc.divide(5, -5) == -1 assert calc.divide(5, 3) == 1 assert calc.divide(5, -3) == -2 assert calc.divide(5, '$') == 'invalid input' assert calc.divide(5, 0) == "Could not calcuate with given input"
def test_divide(self): cases = [ { 'first': 100, 'second': 200, 'expected': 0.5, }, { 'first': 0, 'second': 200, 'expected': 0, }, { 'first': 81, 'second': 9, 'expected': 9, }, { 'first': 81, 'second': -9, 'expected': -9, }, { 'first': -81, 'second': 9, 'expected': -9, }, ] for case in cases: first = case['first'] second = case['second'] self.assertEqual(case['expected'], Calculator.divide(first, second)) self.assertRaises(ZeroDivisionError, Calculator.divide, 25, 0)
class TestCalculator(unittest.TestCase): @classmethod def setUpClass(self): print("Set up class") self.calc = Calculator() # Create an instance of the calculator that can be used in all tests @classmethod def tearDownClass(self): print("Tear down class") def test_add(self): self.assertEqual(self.calc.add(2, 7), 9) # Write test methods for subtract, multiply, and divide def test_subtract(self): self.assertEqual(self.calc.subtract(7, 2), 5) def test_multiply(self): self.assertEqual(self.calc.multiply(2, 4), 8) def test_divide(self): self.assertEqual(self.calc.divide(8, 2), 4)
def test_divide_two_numbers(self): calculator = Calculator() calculator.divide(1,0)
class CalculatorTest(unittest.TestCase): def setUp(self): self.calc = Calculator() def test_last_answer_init(self): value = self.calc.last_answer self.assertEqual(value, 0.0, FAILURE) def test_add(self): value = self.calc.add(NUMBER_1, NUMBER_2) self.assertEqual(value, 5.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_subtract(self): value = self.calc.subtract(NUMBER_1, NUMBER_2) self.assertEqual(value, 1.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_subtract_negative(self): value = self.calc.subtract(NUMBER_2, NUMBER_1) self.assertEqual(value, -1.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_multiply(self): value = self.calc.multiply(NUMBER_1, NUMBER_2) self.assertEqual(value, 6.0, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_divide(self): value = self.calc.divide(NUMBER_1, NUMBER_2) self.assertEqual(value, 1.5, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_divide_by_zero(self): self.assertRaises(ZeroDivisionError, self.calc.divide, NUMBER_1, 0) def test_max_greater(self): value = self.calc.maximum(NUMBER_1, NUMBER_2) self.assertEqual(value, NUMBER_1, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_max_less(self): value = self.calc.maximum(NUMBER_2, NUMBER_1) self.assertEqual(value, NUMBER_1, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_max_equal(self): value = self.calc.maximum(NUMBER_1, NUMBER_1) self.assertEqual(value, NUMBER_1, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_min_greater(self): value = self.calc.minimum(NUMBER_1, NUMBER_2) self.assertEqual(value, NUMBER_2, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_min_less(self): value = self.calc.minimum(NUMBER_2, NUMBER_1) self.assertEqual(value, NUMBER_2, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_min_equal(self): value = self.calc.minimum(NUMBER_2, NUMBER_2) self.assertEqual(value, NUMBER_2, FAILURE) self.assertEqual(value, self.calc.last_answer, FAILURE) def test_force_value(self): self.calc._last_answer = 5 value = self.calc._last_answer self.assertEqual(value, 5, FAILURE) def tearDown(self): pass
def test_divide(self): # Test division self.assertAlmostEqual(Calculator.divide(8, 2), 4) self.assertAlmostEqual(Calculator.divide(9, 2), 4.5) self.assertAlmostEqual(Calculator.divide(5, 4), 1.25)
def test_divide(): calculator = Calculator() assert calculator.divide(5, 1) == 5 assert calculator.divide(9, 3) == 3 assert calculator.divide(9, -3) == -3