如果函数带参数,python assertRaises不会通过测试

时间:2015-06-17 10:43:33

标签: python tdd

assertRaises使用以下代码给出断言错误。有什么我做错了吗?

int ret;
int handle = 0;
int boardId = 0;

Digitizer.ParamStruct Params = new Digitizer.ParamStruct();

Params.LinkType = Digitizer.ConnectionType.conn_USB;
Params.LinkNum = 1;
Params.Node = 0;
Params.BaseAddress = 0;
Params.Address = "";

ret = Digitizer.AddBoard(handle, connParams, ref boardId);

测试似乎通过了以下修改

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))

2 个答案:

答案 0 :(得分:6)

试试这样:

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):

答案 1 :(得分:2)

您需要将可调用而不是结果传递给assertRaises:

self.assertRaises(File_too_small, foo, 1, 2)

或者将其用作上下文管理器:

with self.assertRaises(File_too_small):
    foo(1, 2)