Skip to content

Commit 037bad5

Browse files
committed
Print expected/actual file contents on mismatch
1 parent 7d64bd7 commit 037bad5

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

osgtest/library/osgunittest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
import unittest
1313
import time
1414

15+
16+
# Copied from unittest.util, Python 3.6
17+
_MAX_LENGTH = 80
18+
def safe_repr(obj, short=False):
19+
try:
20+
result = repr(obj)
21+
except Exception:
22+
result = object.__repr__(obj)
23+
if not short or len(result) < _MAX_LENGTH:
24+
return result
25+
return result[:_MAX_LENGTH] + ' [truncated]...'
26+
27+
1528
# Define the classes we need to handle the two new types of test results: ok
1629
# skip, and bad skip.
1730

@@ -111,6 +124,14 @@ def failIfSubsetOf(self, a, b, message=None):
111124
if set(a).issubset(set(b)):
112125
raise AssertionError(message)
113126

127+
def assertEqualVerbose(self, actual, expected, message=None):
128+
aftermessage = "actual %s != expected %s" % (safe_repr(actual), safe_repr(expected))
129+
if message:
130+
fullmessage = "%s (%s)" % (message, aftermessage)
131+
else:
132+
fullmessage = aftermessage
133+
self.assertEqual(actual, expected, fullmessage)
134+
114135
# This is mostly a copy of the method from unittest in python 2.4.
115136
# There is some code here to test if the 'result' object accepts 'skips',
116137
# since the original TestResult object does not. If it does not, an

osgtest/tests/test_460_stashcache.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ def assertCached(self, name, contents):
3333
fpath = os.path.join(_getcfg("cache_dir"), name)
3434
self.assertTrue(os.path.exists(fpath),
3535
name + " not cached")
36-
self.assertEqual(contents, files.read(fpath, as_single_string=True),
37-
"cached file %s contents do not match expected" % name)
36+
self.assertEqualVerbose(actual=files.read(fpath, as_single_string=True),
37+
expected=contents,
38+
message="cached file %s mismatch" % name)
3839

3940
def skip_bad_unless_running(self, *services):
4041
for svc in services:
@@ -58,7 +59,7 @@ def test_02_xroot_fetch_from_origin(self):
5859
core.check_system(["xrdcp", "-d1", "-N", "-f",
5960
"root://localhost:%d//%s" % (_getcfg("origin_xroot_port"), name),
6061
"-"], "Checking xroot copy from origin")
61-
self.assertEqual(result, contents, "downloaded file does not match expected")
62+
self.assertEqualVerbose(result, contents, "downloaded file mismatch")
6263

6364
def test_03_http_fetch_from_cache(self):
6465
name, contents = self.testfiles[1]
@@ -69,7 +70,7 @@ def test_03_http_fetch_from_cache(self):
6970
result = f.read()
7071
except IOError as e:
7172
self.fail("Unable to download from cache via http: %s" % e)
72-
self.assertEqual(result, contents, "downloaded file does not match expected")
73+
self.assertEqualVerbose(result, contents, "downloaded file mismatch")
7374
self.assertCached(name, contents)
7475

7576
def test_04_xroot_fetch_from_cache(self):
@@ -78,7 +79,7 @@ def test_04_xroot_fetch_from_cache(self):
7879
core.check_system(["xrdcp", "-d1", "-N", "-f",
7980
"root://localhost:%d//%s" % (_getcfg("cache_xroot_port"), name),
8081
"-"], "Checking xroot copy from cache")
81-
self.assertEqual(result, contents, "downloaded file does not match expected")
82+
self.assertEqualVerbose(result, contents, "downloaded file mismatch")
8283
self.assertCached(name, contents)
8384

8485
def test_05_stashcp(self):
@@ -90,5 +91,5 @@ def test_05_stashcp(self):
9091
core.check_system(command + ["/"+name, tf.name],
9192
"Checking stashcp")
9293
result = tf.read()
93-
self.assertEqual(result, contents, "stashcp'ed file does match expected")
94+
self.assertEqualVerbose(result, contents, "stashcp'ed file mismatch")
9495
self.assertCached(name, contents)

0 commit comments

Comments
 (0)