summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test/coveragetest.py19
-rw-r--r--test/test_testing.py5
2 files changed, 23 insertions, 1 deletions
diff --git a/test/coveragetest.py b/test/coveragetest.py
index d1631a3d..b86794d4 100644
--- a/test/coveragetest.py
+++ b/test/coveragetest.py
@@ -1,6 +1,6 @@
"""Base test case class for coverage testing."""
-import imp, os, random, re, shutil, sys, tempfile, textwrap, unittest
+import difflib, imp, os, random, re, shutil, sys, tempfile, textwrap, unittest
import coverage
from coverage.backward import set, sorted, StringIO # pylint: disable-msg=W0622
@@ -280,3 +280,20 @@ class CoverageTest(unittest.TestCase):
m = re.search(regex, s)
if not m:
raise self.failureException("%r doesn't match %r" % (s, regex))
+
+ def assert_multiline_equal(self, first, second):
+ """Assert that two multi-line strings are equal.
+
+ If they aren't, show a nice diff.
+
+ """
+ # Adapted from Py3.1 unittest.
+ self.assert_(isinstance(first, str), (
+ 'First argument is not a string'))
+ self.assert_(isinstance(second, str), (
+ 'Second argument is not a string'))
+
+ if first != second:
+ msg = ''.join(difflib.ndiff(first.splitlines(True),
+ second.splitlines(True)))
+ self.fail("Multi-line strings are unequal:\n" + msg)
diff --git a/test/test_testing.py b/test/test_testing.py
index 1e22202a..5d1ac0bc 100644
--- a/test/test_testing.py
+++ b/test/test_testing.py
@@ -28,3 +28,8 @@ class TestingTest(CoverageTest):
"hello there", "^hello$"
)
+ def test_assert_multiline_equal(self):
+ self.assert_multiline_equal("hello", "hello")
+ self.assertRaises(AssertionError, self.assert_matches,
+ "hello there", "Hello there"
+ )