class TddInPythonExample(unittest.TestCase): def setUp(self): self.calc = Calculator() def test_calculator_add_method_returns_correct_result(self): result = self.calc.add(2, 2) self.assertEqual(43, result) def test_calculator_returns_error_message_if_both_args_not_numbers(self): self.assertRaises(ValueError, self.calc.add, 'two', 'three') def test_calculator_returns_error_message_if_x_arg_not_number(self): self.assertRaises(ValueError, self.calc.add, 'two', 3) def test_calculator_returns_error_message_if_y_arg_not_number(self): self.assertRaises(ValueError, self.calc.add, 2, 'three')
class CalculatorTests(unittest.TestCase): def setUp(self): self.calculator = Calculator() def test_calculator_should_instantiate(self): self.assertIsInstance(self.calculator, Calculator) def test_add_should_return_sum_when_given_two_integers(self): result = self.calculator.add(4, 5) expected = 9 self.assertEqual(result, expected) def test_add_should_return_ValueError_when_given_two_strings(self): self.assertRaises(ValueError, self.calculator.add, "ten", "twenty") def test_multiply_should_multiply_numbers_when_given_two_integers(self): result = self.calculator.multiply(3, 5) expected = 15 self.assertEqual(result, expected) def test_multiply_should_return_ValueError_when_given_two_strings(self): self.assertRaises(ValueError, self.calculator.multiply, "five", "ten") def test_subtract_should_subtract_numbers_when_given_two_integers(self): result = self.calculator.subtract(5, 2) expected = 3 self.assertEqual(result, expected) def test_subtract_should_return_ValueError_when_given_two_strings(self): self.assertRaises(ValueError, self.calculator.subtract, "fourty", "one") def test_divide_should_divide_numbers_when_given_two_integers(self): result = self.calculator.divide(10, 5) expected = 2 self.assertEqual(result, expected) def test_divide_should_return_ValueError_when_given_two_strings(self): self.assertRaises(ValueError, self.calculator.divide, "fifteen", "three")
class Tests(unittest.TestCase): def setUp(self): self.calculator = Calculator() def test_calculator_object(self): self.assertIsInstance(self.calculator, Calculator) def test_add_function_exists(self): self.calculator.add def test_add_two_numbers(self): result = self.calculator.add(10,2) expected = 12 self.assertEqual(result, expected) def test_subtract_function_exists(self): self.calculator.subtract def test_subtract_two_numbers(self): result = self.calculator.subtract(10,2) expected = 8 self.assertEqual(result, expected) def test_divide_function_exists(self): self.calculator.divide def test_divide_two_numbers(self): result = self.calculator.divide(10,2) expected = 5 self.assertEqual(result, expected) def test_multiply_function_exists(self): self.calculator.multiply def test_multiply_two_numbers(self): result = self.calculator.multiply(10,2) expected = 20 self.assertEqual(result, expected)
def test_add(): assert Calculator.add(numbers=[3], balance=3) == 6 assert Calculator.add(numbers=[0], balance=0) == 0
def test_calculator_add_method(self): calc = Calculator() result = calc.add(2, 2) self.assertEqual(4, result)
class AppTests(unittest.TestCase): def setUp(self): self.calculator = Calculator() def test_calculator_instantiates(self): self.assertIsInstance(self.calculator, Calculator) def test_add_sums_when_numbers(self): result = self.calculator.add(2, 2) expected = 4 self.assertEqual(result, expected) def test_add_rejects_when_string(self): self.assertRaises(ValueError, self.calculator.add, 2, "two") def test_subtract_subtracts_when_numbers(self): result = self.calculator.subtract(2, 2) expected = 0 self.assertEqual(result, expected) def test_subtract_rejects_when_string(self): self.assertRaises(ValueError, self.calculator.subtract, 2, "two") def test_multiply_multiplies_when_numbers(self): result = self.calculator.multiply(2, 2) expected = 4 self.assertEqual(result, expected) def test_multiply_rejects_when_string(self): self.assertRaises(ValueError, self.calculator.add, 2, "two") def test_divide_divides_numbers_when_whole(self): result = self.calculator.divide(2, 2) expected = 1 self.assertEqual(result, expected) def test_divide_returns_decimal_when_not_whole(self): result = self.calculator.divide(3, 2) expected = 1.5 self.assertEqual(result, expected) def test_divide_rejects_when_string(self): self.assertRaises(ValueError, self.calculator.divide, 2, "two") def test_divide_rejects_when_divisor_is_zero(self): self.assertRaises(ZeroDivisionError, self.calculator.divide, 2, 0) def test_simple_divide_returns_quotient_when_whole(self): result = self.calculator.simple_divide(4, 2) expected = 2 self.assertEqual(result, expected) def test_simple_divide_returns_quotient_and_remainder_when_not_whole(self): result = self.calculator.simple_divide(5, 2) expected = 2, 1 self.assertEqual(result, expected) def test_simple_divide_rejects_when_string(self): self.assertRaises(ValueError, self.calculator.simple_divide, 5, "two") def test_simple_divide_rejeects_when_divisor_is_zero(self): self.assertRaises(ZeroDivisionError, self.calculator.simple_divide, 5, 0)