python assertRaises 没有通过带参数函数的测试

python assertRaises don't pass test if function with parameters

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,foo(1,2))

测试似乎通过了以下修改

def foo:
    raise File_too_small

def test_foo(self):
    self.assertRaises(File_too_small,foo)

您需要将可调用对象而不是结果传递给 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):
    self.assertRaises(File_too_small, foo, 1, 2):