Why isn't assertRaises catching my Attribute Error using python unittest?

后端 未结 3 2055
盖世英雄少女心
盖世英雄少女心 2020-12-29 04:06

I\'m trying to run this test: self.assertRaises(AttributeError, branch[0].childrennodes), and branch[0] does not have an attribute childrenno

3条回答
  •  死守一世寂寞
    2020-12-29 05:04

    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.

    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
    

提交回复
热议问题