我的课程和方法如下:
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
我想编写一个单元测试来检查给定的参数是否是正确的类型。当你输入会生气时,抛出一个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)
当给出正确类型的测试参数时,应该显示错误,而不是。为什么? 请求帮助/一些提示