Skip to main content
edited tags
Link
Charles
  • 51.5k
  • 13
  • 107
  • 146
Source Link
Jonas Kölker
  • 7.8k
  • 3
  • 46
  • 52

How do I signal test error (not failure) from python unittest

I have a test case with a helper method assertContains(super, sub). The sub arguments are a hard-coded part of the test cases. In case they're malformed, I would like my test case to abort with an error.

How do I do that? I have tried

def assertContains(super, sub):
    if isinstance(super, foo): ...
    elif isinstance(super, bar): ...
    else: assert False, repr(sub)

However, this turns the test into a failure rather than an error.

I could raise some other exception (e.g. ValueError), but I want to explicitly state that I'm declaring the test case to be in error. I could do things like ErrorInTest = ValueError and then raise ErrorInTest(repr(sub)), but it feels kinda' icky. I feel there should be a batteries-included way of doing this, but reading the friendly manual didn't suggest anything to me.