python-asserton';如果功能带有参数,则无法通过测试
assertRaises使用以下代码给出断言错误。我做错什么了吗python-asserton';如果功能带有参数,则无法通过测试,python,tdd,Python,Tdd,assertRaises使用以下代码给出断言错误。我做错什么了吗 class File_too_small(Exception): "Check file size" def foo(a,b): if a<b: raise File_too_small class some_Test(unittest.TestCase): def test_foo(self): self.assertRaises(File_too_small,f
class File_too_small(Exception):
"Check file size"
def foo(a,b):
if a<b:
raise File_too_small
class some_Test(unittest.TestCase):
def test_foo(self):
self.assertRaises(File_too_small,foo(1,2))
您需要将可调用而不是结果传递给assertRaises:
self.assertRaises(File_too_small, foo, 1, 2)
或者将其用作上下文管理器:
with self.assertRaises(File_too_small):
foo(1, 2)
试着这样做:
def test_foo(self):
with self.assertRaises(File_too_small):
foo(1, 2)
或:
def test_foo(self):
with self.assertRaises(File_too_small):
foo(1, 2)
def test_foo(self):
self.assertRaises(File_too_small, foo, 1, 2):