Python 2.7. The unittest doc says:
To make migrating existing test suites easier, unittest supports tests raising AssertionError to indicate test failure. However, it is recommended that you use the explicit TestCase.fail*() and TestCase.assert*() methods instead, as future versions of unittest may treat AssertionError differently.
I used quite a few assert statements inside the tested code, but failure of those assertions should really be a test error (i.e. "code did not run properly with those inputs") rather than a failure (i.e. "code gave incorrect output").
I can see the following possible solutions:
- Rewrite the tested code to throw better-typed exceptions
- Wrap everything inside test methods except the test assertions themselves (
self.assertSomething(...)) intry...except AssertionError: raise SomeOtherExceptionblocks - Change unittest's behavior so that it considers those errors rather than failures.
Option 1 would take quite some time, and option 2 feels hacky; option 3 would be the best for me, but is it available? (In case it matters: no, I cannot switch to Python 3.) I do not see anything online, but it is hard to use specific keywords.
MWE:
import unittest
def add_one_to_int(a):
assert isinstance(a, int)
return a + 1
class TestAddOne(unittest.TestCase):
def test_one_plus_one_is_three(self):
# This tests fails with
# AssertionError: 2 != 3
# which is fine
self.assertEqual(add_one_to_int(1), 3)
def test_add_one_to_str(self):
# This tests fails with
# AssertionError
# when I would rather have it an error
add_one_to_int('some string')
if __name__ == '__main__':
unittest.main(verbosity=2) # 2 failures instead of 1 failure, 1 error