title: Python unittest: make AssertionError an error instead of a failure
body: <p>Python 2.7. <a href="https://docs.python.org/2/library/unittest.html#re-using-old-test-code" rel="nofollow noreferrer">The unittest doc</a> says:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>I used quite a few <code>assert</code> 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"). </p>
  
  <p>I can see the following possible solutions:</p>
  
  <ol>
  <li>Rewrite the tested code to throw better-typed exceptions</li>
  <li>Wrap everything inside test methods except the test assertions themselves (<code>self.assertSomething(...)</code>) in <code>try...except AssertionError: raise SomeOtherException</code> blocks</li>
  <li>Change unittest's behavior so that it considers those errors rather than failures.</li>
  </ol>
  
  <p>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.</p>
  
  <p>MWE:</p>
  
  <pre class="lang-py prettyprint-override"><code>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
  </code></pre>