mirror of
https://github.com/samba-team/samba.git
synced 2025-01-27 14:04:05 +03:00
python tests: assert string equality, with diff
In the success case this works just like self.assertEqual(), but when things fail you get a better representation of where it went wrong (a unified diff). Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
parent
3f2762d0b7
commit
f2762d0880
@ -216,6 +216,29 @@ class TestCase(unittest.TestCase):
|
|||||||
finally:
|
finally:
|
||||||
result.stopTest(self)
|
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):
|
class LdbTestCase(TestCase):
|
||||||
"""Trivial test case for running tests against a LDB."""
|
"""Trivial test case for running tests against a LDB."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user