python tests: assert string equality, with diff
[samba.git] / python / samba / tests / __init__.py
index d634acab236e32430fd141bcdfedf47d878e533e..c4d24fa913f0ba641b263c36eea2c1ff6c635215 100644 (file)
@@ -216,6 +216,29 @@ class TestCase(unittest.TestCase):
             finally:
                 result.stopTest(self)
 
+    def assertStringsEqual(self, a, b, msg=None, strip=False):
+        """Assert equality between two strings and highlight any differences.
+        If strip is true, leading and trailing whitespace is ignored."""
+        if strip:
+            a = a.strip()
+            b = b.strip()
+
+        if a != b:
+            sys.stderr.write("The strings differ %s(lengths %d vs %d); "
+                             "a diff follows\n"
+                             % ('when stripped ' if strip else '',
+                                len(a), len(b),
+                             ))
+
+            from difflib import unified_diff
+            diff = unified_diff(a.splitlines(True),
+                                b.splitlines(True),
+                                'a', 'b')
+            for line in diff:
+                sys.stderr.write(line)
+
+            self.fail(msg)
+
 
 class LdbTestCase(TestCase):
     """Trivial test case for running tests against a LDB."""