为什么assertRaises没有使用python unittest捕获我的属性错误?

时间:2022-11-24 09:13:20

I'm trying to run this test: self.assertRaises(AttributeError, branch[0].childrennodes), and branch[0] does not have an attribute childrennodes, so it should be throwing an AttributeError, which the assertRaises should catch, but when I run the test, the test fails because it is throwing an AttributeError.

我正在尝试运行此测试:self.assertRaises(AttributeError,branch [0] .childrennodes),而branch [0]没有属性childrennodes,所以它应该抛出一个AttributeError,assertRaises应该捕获它,但是当我运行测试时,测试失败,因为它抛出了一个AttributeError。

Traceback (most recent call last):
  File "/home/tttt/../tttt/tests.py", line 504, in test_get_categories_branch
    self.assertRaises(AttributeError, branch[0].children_nodes)
AttributeError: 'Category' object has no attribute 'children_nodes'

Any ideas?

有任何想法吗?

3 个解决方案

#1


19  

I think its because assert raises only accepts a callable. It evalutes to see if the callable raises an exception, not if the statement itself does.

我认为这是因为断言加注只接受一个可调用的。它评估是否可调用引发异常,而不是语句本身。

self.assertRaises(AttributeError, getattr, branch[0], "childrennodes")

should work.

应该管用。

EDIT:

编辑:

As THC4k correctly says it gathers the statements at collection time and will error then, not at testing time.

正如THC4k正确地说它在收集时收集语句然后会出错,而不是在测试时。

Also this is a reason why I like nose, it has a decorator (raises) that is useful and clearer for these kind of tests.

这也是我喜欢鼻子的原因,它有一个装饰器(加注),这对于这类测试是有用和清晰的。

@raises(AttributeError)
def test_1(self)
    branch[0].childrennodes

#2


50  

When the test is running, before calling self.assertRaises, Python needs to find the value of all the method's arguments. In doing so, it evaluates branch[0].children_nodes, which raises an AttributeError. Since we haven't invoked assertRaises yet, this exception is not caught, causing the test to fail.

当测试运行时,在调用self.assertRaises之前,Python需要找到所有方法参数的值。在这样做时,它会计算branch [0] .children_nodes,它会引发AttributeError。由于我们尚未调用assertRaises,因此未捕获此异常,导致测试失败。

The solution is to wrap branch[0].children_nodes in a function or a lambda:

解决方案是将branch [0] .children_nodes包装在函数或lambda中:

self.assertRaises(AttributeError, lambda: branch[0].children_nodes)

assertRaises can also be used as a context manager (Since Python 2.7, or in PyPI package 'unittest2'):

assertRaises也可以用作上下文管理器(从Python 2.7开始,或者在PyPI包'unittest2'中):

with self.assertRaises(AttributeError):
    branch[0].children_nodes
    # etc

This is nice because it can be used on arbitrary blocks of code in the middle of a test, rather than having to create a new function just to define the block of code to which it applies.

这很好,因为它可以在测试过程中用于任意代码块,而不是仅仅为了定义它所适用的代码块而创建一个新函数。

It can give you access to the raised exception for further processing, if needed:

如果需要,它可以让您访问引发的异常以进行进一步处理:

with self.assertRaises(AttributeError) as cm:
    branch[0].children_nodes

self.assertEquals(cm.exception.special_attribute, 123)

#3


1  

pytest also has a similar decorator:

pytest也有类似的装饰:

from pytest import raises

def test_raising():
    with raises(AttributeError):
        branch[0].childrennodes

#1


19  

I think its because assert raises only accepts a callable. It evalutes to see if the callable raises an exception, not if the statement itself does.

我认为这是因为断言加注只接受一个可调用的。它评估是否可调用引发异常,而不是语句本身。

self.assertRaises(AttributeError, getattr, branch[0], "childrennodes")

should work.

应该管用。

EDIT:

编辑:

As THC4k correctly says it gathers the statements at collection time and will error then, not at testing time.

正如THC4k正确地说它在收集时收集语句然后会出错,而不是在测试时。

Also this is a reason why I like nose, it has a decorator (raises) that is useful and clearer for these kind of tests.

这也是我喜欢鼻子的原因,它有一个装饰器(加注),这对于这类测试是有用和清晰的。

@raises(AttributeError)
def test_1(self)
    branch[0].childrennodes

#2


50  

When the test is running, before calling self.assertRaises, Python needs to find the value of all the method's arguments. In doing so, it evaluates branch[0].children_nodes, which raises an AttributeError. Since we haven't invoked assertRaises yet, this exception is not caught, causing the test to fail.

当测试运行时,在调用self.assertRaises之前,Python需要找到所有方法参数的值。在这样做时,它会计算branch [0] .children_nodes,它会引发AttributeError。由于我们尚未调用assertRaises,因此未捕获此异常,导致测试失败。

The solution is to wrap branch[0].children_nodes in a function or a lambda:

解决方案是将branch [0] .children_nodes包装在函数或lambda中:

self.assertRaises(AttributeError, lambda: branch[0].children_nodes)

assertRaises can also be used as a context manager (Since Python 2.7, or in PyPI package 'unittest2'):

assertRaises也可以用作上下文管理器(从Python 2.7开始,或者在PyPI包'unittest2'中):

with self.assertRaises(AttributeError):
    branch[0].children_nodes
    # etc

This is nice because it can be used on arbitrary blocks of code in the middle of a test, rather than having to create a new function just to define the block of code to which it applies.

这很好,因为它可以在测试过程中用于任意代码块,而不是仅仅为了定义它所适用的代码块而创建一个新函数。

It can give you access to the raised exception for further processing, if needed:

如果需要,它可以让您访问引发的异常以进行进一步处理:

with self.assertRaises(AttributeError) as cm:
    branch[0].children_nodes

self.assertEquals(cm.exception.special_attribute, 123)

#3


1  

pytest also has a similar decorator:

pytest也有类似的装饰:

from pytest import raises

def test_raising():
    with raises(AttributeError):
        branch[0].childrennodes