1

I have class and method as below:

class Calculator(object):

    def add(self, x, y):
        try:
            if not isinstance(x, int) or not isinstance(y, int):
                raise TypeError
        except TypeError:
            print "Parametr has wrong type"
        else:
            return x + y

I want to write a unit test that checks if the given parameters are correct type. When you type will be angry, throw a TypeError.

import unittest
from Calculator import Calculator

class TDD_in_python_example(unittest.TestCase):
    def test_add_correct(self):
        calc = Calculator()
        result = calc.add(2, 3)
        self.assertEqual(5, result)

    def test_when_type_is_ok(self):
        calc = Calculator()
        result = calc.add(2, 3)
        self.assertRaises(TypeError, result, 2, 3)

    def test_when_type_of_parameter_is_wrong(self):
        calc = Calculator()
        result = calc.add(2, 3)
        self.assertRaises(TypeError, result, "two", "three")

    def test_when_one_type_of_parameter_is_wrong(self):
        calc = Calculator()
        result = calc.add(2, 3)
        self.assertRaises(TypeError, result, 2, "three")

    def test_when_two_type_of_parameter_is_wrong(self):
        calc = Calculator()
        result = calc.add(2, 3)
        self.assertRaises(TypeError, result, 'two', 3)

When given the correct type of test parameters should show the error, and it is not. Why? Please for help/some hint

4

0 回答 0