Skip to main content
Added the common method and some small things
Source Link
MeteHan
  • 350
  • 1
  • 2
  • 9
import Calculator
import unittest


class CalculatorTests(unittest.TestCase):

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

    def test_add_method(self):
        result = self.ccalculator.add(4, 2)
        self.assertEqual(6, result)

    def test_add_method_invalid_value(self):
        self.assertRaises(ValueError, self.ccalculator.add, "four", "five")

    def test_multiply_method(self):
        result = self.ccalculator.multiply(5, 3)
        self.assertEqual(15, result)

    def test_multiply_method_invalid_value(self):
        self.assertRaises(ValueError, self.ccalculator.multiply, "four", "five")

    def test_sub_method(self):
        result = self.ccalculator.sub(6, 4)
        self.assertEqual(2, result)

    def test_sub_method_invalid_value(self):
        self.assertRaises(ValueError, self.ccalculator.sub, "four", "five")

    def test_div_method(self):
        result = self.ccalculator.div(5, 1)
        self.assertEqual(5, result)

    def test_div_method_invalid_value(self):
        self.assertRaises(ValueError, self.ccalculator.div, "five", "four")

    def test_div_method_zero(self):
        self.assertRaises(ZeroDivisionError, self.ccalculator.div, 5, 0) 


if __name__ == '__main__':
    unittest.main()
class Calculator:

    def __init__(self):
        self.number_types = (int, float, complex)


class Calculator:

    @staticmethod
    def addvalidate_args(self, x, y):
        if not isinstance(x, self.number_types) and not isinstance(y, self.number_types):
            return x + y
        else:
            raise ValueError

    def multiplyadd(self, x, y):
        if isinstance(x, self.number_types) and isinstancevalidate_args(yx, self.number_typesy):
          return x return+ x*yy
  
    def multiply(self, x, elsey):
        self.validate_args(x, y)
        raisereturn ValueErrorx*y

    def sub(self, x, y):
        if isinstance(x, self.number_types) and isinstancevalidate_args(yx, self.number_typesy):
            return x - y
        else:
            raise ValueError

    def div(self, x, y):
        if isinstance(x, self.number_types) and isinstancevalidate_args(yx, self.number_typesy):
            return x/y
        else:
            raise ValueError
import Calculator
import unittest


class CalculatorTests(unittest.TestCase):

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

    def test_add_method(self):
        result = self.c.add(4, 2)
        self.assertEqual(6, result)

    def test_add_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.add, "four", "five")

    def test_multiply_method(self):
        result = self.c.multiply(5, 3)
        self.assertEqual(15, result)

    def test_multiply_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.multiply, "four", "five")

    def test_sub_method(self):
        result = self.c.sub(6, 4)
        self.assertEqual(2, result)

    def test_sub_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.sub, "four", "five")

    def test_div_method(self):
        result = self.c.div(5, 1)
        self.assertEqual(5, result)

    def test_div_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.div, "five", "four")

    def test_div_method_zero(self):
        self.assertRaises(ZeroDivisionError, self.c.div, 5, 0)

if __name__ == '__main__':
    unittest.main()
class Calculator:

    def __init__(self):
        self.number_types = (int, float, complex)

    def add(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x + y
        else:
            raise ValueError

    def multiply(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x*y
        else:
            raise ValueError

    def sub(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x - y
        else:
            raise ValueError

    def div(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x/y
        else:
            raise ValueError
import Calculator
import unittest


class CalculatorTests(unittest.TestCase):

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

    def test_add_method(self):
        result = self.calculator.add(4, 2)
        self.assertEqual(6, result)

    def test_add_method_invalid_value(self):
        self.assertRaises(ValueError, self.calculator.add, "four", "five")

    def test_multiply_method(self):
        result = self.calculator.multiply(5, 3)
        self.assertEqual(15, result)

    def test_multiply_method_invalid_value(self):
        self.assertRaises(ValueError, self.calculator.multiply, "four", "five")

    def test_sub_method(self):
        result = self.calculator.sub(6, 4)
        self.assertEqual(2, result)

    def test_sub_method_invalid_value(self):
        self.assertRaises(ValueError, self.calculator.sub, "four", "five")

    def test_div_method(self):
        result = self.calculator.div(5, 1)
        self.assertEqual(5, result)

    def test_div_method_invalid_value(self):
        self.assertRaises(ValueError, self.calculator.div, "five", "four")

    def test_div_method_zero(self):
        self.assertRaises(ZeroDivisionError, self.calculator.div, 5, 0) 


if __name__ == '__main__':
    unittest.main()
number_types = (int, float, complex)


class Calculator:

    @staticmethod
    def validate_args(x, y):
        if not isinstance(x, number_types) and not isinstance(y, number_types):
            raise ValueError

    def add(self, x, y):
        self.validate_args(x, y)
        return x + y
 
    def multiply(self, x, y):
        self.validate_args(x, y)
        return x*y

    def sub(self, x, y):
        self.validate_args(x, y)
        return x-y

    def div(self, x, y):
        self.validate_args(x, y)
        return x/y
Source Link
MeteHan
  • 350
  • 1
  • 2
  • 9

Python Calculator Test Driven Development

I am a beginner in TDD and I just coded a Calculator in Python with TDD approach. I would be more than happy to hear your opinions out. And since I am a beginner in TDD, I would love to hear some tips.

Tests :

import Calculator
import unittest


class CalculatorTests(unittest.TestCase):

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

    def test_add_method(self):
        result = self.c.add(4, 2)
        self.assertEqual(6, result)

    def test_add_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.add, "four", "five")

    def test_multiply_method(self):
        result = self.c.multiply(5, 3)
        self.assertEqual(15, result)

    def test_multiply_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.multiply, "four", "five")

    def test_sub_method(self):
        result = self.c.sub(6, 4)
        self.assertEqual(2, result)

    def test_sub_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.sub, "four", "five")

    def test_div_method(self):
        result = self.c.div(5, 1)
        self.assertEqual(5, result)

    def test_div_method_invalid_value(self):
        self.assertRaises(ValueError, self.c.div, "five", "four")

    def test_div_method_zero(self):
        self.assertRaises(ZeroDivisionError, self.c.div, 5, 0)

if __name__ == '__main__':
    unittest.main()

Calculator.py :

class Calculator:

    def __init__(self):
        self.number_types = (int, float, complex)

    def add(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x + y
        else:
            raise ValueError

    def multiply(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x*y
        else:
            raise ValueError

    def sub(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x - y
        else:
            raise ValueError

    def div(self, x, y):
        if isinstance(x, self.number_types) and isinstance(y, self.number_types):
            return x/y
        else:
            raise ValueError