diff options
author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> | 2008-05-01 17:27:22 +0200 |
---|---|---|
committer | Alexandre Fayolle <alexandre.fayolle@logilab.fr> | 2008-05-01 17:27:22 +0200 |
commit | ea31e8df6ca8008fbb7db11ec9e181c14f2a672b (patch) | |
tree | 7985184648edc6d58ac911754a3d5d0437153465 | |
parent | c342713699ce09524ba7710410deb40298fedacb (diff) | |
download | logilab-common-ea31e8df6ca8008fbb7db11ec9e181c14f2a672b.tar.gz |
support a msg argument on more assertions
-rw-r--r-- | testlib.py | 33 |
1 files changed, 20 insertions, 13 deletions
@@ -1089,7 +1089,7 @@ class TestCase(unittest.TestCase): self.fail('\n'.join(msgs)) assertDictEqual = assertDictEquals - def assertSetEquals(self, got, expected): + def assertSetEquals(self, got, expected, msg=None): """compares two iterables and shows difference between both""" got, expected = list(got), list(expected) self.assertEquals(len(got), len(expected)) @@ -1097,11 +1097,13 @@ class TestCase(unittest.TestCase): if got != expected: missing = expected - got unexpected = got - expected - self.fail('\tunexepected: %s\n\tmissing: %s' % (unexpected, - missing)) + if msg is None: + msg = '\tunexepected: %s\n\tmissing: %s' % (unexpected, + missing) + self.fail(msg) assertSetEqual = assertSetEquals - def assertListEquals(self, l1, l2): + def assertListEquals(self, l1, l2, msg=None): """compares two lists If the two list differ, the first difference is shown in the error @@ -1117,32 +1119,37 @@ class TestCase(unittest.TestCase): self.fail('%r != %r for index %d' % (_l1[0], value, i)) del _l1[0] except IndexError: - msg = 'l1 has only %d elements, not %s (at least %r missing)' - self.fail(msg % (i, len(l2), value)) + if msg is None: + msg = 'l1 has only %d elements, not %s (at least %r missing)'% (i, len(l2), value) + self.fail(msg) if _l1: - self.fail('l2 is lacking %r' % _l1) + if msg is None: + msg = 'l2 is lacking %r' % _l1 + self.fail(msg) assertListEqual = assertListEquals - def assertLinesEquals(self, l1, l2): + def assertLinesEquals(self, l1, l2, msg=None): """assert list of lines are equal""" - self.assertListEquals(l1.splitlines(), l2.splitlines()) + self.assertListEquals(l1.splitlines(), l2.splitlines(), msg) assertLineEqual = assertLinesEquals - def assertXMLWellFormed(self, stream): + def assertXMLWellFormed(self, stream, msg=None): """asserts the XML stream is well-formed (no DTD conformance check)""" from xml.sax import make_parser, SAXParseException parser = make_parser() try: parser.parse(stream) except SAXParseException: - self.fail('XML stream not well formed') + if msg is None: + msg = 'XML stream not well formed' + self.fail(msg) assertXMLValid = deprecated_function(assertXMLWellFormed, 'assertXMLValid renamed to more precise assertXMLWellFormed') - def assertXMLStringWellFormed(self, xml_string): + def assertXMLStringWellFormed(self, xml_string, msg=None): """asserts the XML string is well-formed (no DTD conformance check)""" stream = StringIO(xml_string) - self.assertXMLWellFormed(stream) + self.assertXMLWellFormed(stream, msg) assertXMLStringValid = deprecated_function( assertXMLStringWellFormed, 'assertXMLStringValid renamed to more precise assertXMLStringWellFormed') |