1

Using Python's unittest module, the assertion

self.assertTrue(a > b - 0.5 && a < b + 0.5, "The two values did not agree")

outputs the following on failure:

AssertionError: False is not true : The two values did not agree

I don't want the False is not true to be printed. Ideally, AssertionError shouldn't be printed either. Only The two values did not agree should be printed.

Can I do this?

1 Answer 1

5

You can supress the False is true part, however, one thing to keep in mind here, you are raising an exception, and what you are seeing is the standard output of an assertion being raised in Python. You want to see this. Furthermore, this is being called directly in the assertion methods, as you can see from the assertTrue below:

def assertTrue(self, expr, msg=None):
    """Check that the expression is true."""
    if not expr:
        msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))
        raise self.failureException(msg)

To supress the 'False is true' part, change the longMessage class attribute to False:

class TestCompare(unittest.TestCase):

    longMessage = False

    def test_thing(self):
        self.assertTrue(5 == 6, "The two values did not agree")

Output:

Failure
Traceback (most recent call last):
  File "/Users/XXX/dev/rough/test_this.py", line 21, in test_things
    self.assertTrue(5 == 6, "The two values did not agree")
AssertionError: The two values did not agree
Sign up to request clarification or add additional context in comments.

1 Comment

@Tyler No problem. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.