Suppress supplementary error message from unit test

Dhara

I am using assertRegexpMatches in my unittests:

self.assertRegexpMatches(text, regexp, msg='custom short message')

The problem is that unittest adds it's own error message to the msg specified as argument:

AssertionError: <custom short message>: '<regexp>' not found in '<text>'

Since the text to match is quite large (~1 page), it messes up the test reports. Is there any way to suppress unittest from adding the '<regexp>' not found in '<text>' part to the specified error message?

falsetru

According to the source code, it's impossible to suppress the message as long as you use TestCase.assertRegexpMatches.

def assertRegexpMatches(self, text, expected_regexp, msg=None):
    """Fail the test unless the text matches the regular expression."""
    if isinstance(expected_regexp, basestring):
        expected_regexp = re.compile(expected_regexp)
    if not expected_regexp.search(text):
        msg = msg or "Regexp didn't match"
        msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text) #<-
        raise self.failureException(msg)

You need to define your own assertion method or use custom string class as follow:

Example (not beautiful solution, but works):

import unittest

class CustomString(str):
    # XXX: implementation dependant
    # redefine `__repr__` because `assertRegexpMatches` use `%r`
    def __repr__(self):
        return '<Huge string>'

class TestFoo(unittest.TestCase):
    def test_foo(self):
        self.assertRegexpMatches(CustomString('1234'), 'abcd', msg='custom msg')

if __name__ == '__main__':
    unittest.main()

or using re.search with assertTrue:

class TestFoo(unittest.TestCase):
    def test_foo(self):
        self.assertTrue(re.search(regexp, text), msg='custom msg')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

VSTS Unit test error message

From Dev

Suppress error from Unit of Work transaction

From Dev

tryCatch suppress error message

From Dev

Suppress error message in R

From Dev

Suppress error message in R

From Dev

Can i suppress error message from fetch.pm in Perl

From Dev

How to suppress [no test files] message on go test

From Dev

How to suppress error message of a command?

From Dev

How to suppress OpenCV error message

From Dev

matlab - suppress error message backtrace

From Dev

Mocha Unit Test Error Handling from Library

From Dev

unable to get error from SeeJson unit test

From Dev

ActiveMQ Message Grouping Unit Test

From Dev

JAX-RS : Suppress Error Message

From Dev

Not able to suppress the error message using /dev/null

From Dev

How to suppress certain error message in 'find' command?

From Dev

OCMock unit test error

From Dev

Suppress an error from the logs too

From Dev

Unit test error : This function can only be invoked from LINQ to Entities

From Dev

Suppress output from google test assertions

From Dev

in unit testing it gives this error message

From Dev

How can I write a unit test assertion that checks for an error with specified identifier and with a specific message?

From Dev

mockMvc - Test Error Message

From Dev

AngularJS Unit Test Error dependence

From Dev

Forms Suppress Error Message And Catch frm-40350

From Dev

Numpy / Polyfit - Suppress printing of Intel MKL Error message

From Dev

Forms Suppress Error Message And Catch frm-40350

From Dev

Numpy / Polyfit - Suppress printing of Intel MKL Error message

From Dev

How to suppress PowerShell error message during variable assignment

Related Related

HotTag

Archive