Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Allow adding optional message to failed assert calls #1347

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tests/dbus-tests/test_40_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def read_sys_file(value):

for prop_name, expected_val in expected_prop_vals.items():
actual_val = self.get_property(self.cd_drive, '.Drive', prop_name)
actual_val.assertEqual(expected_val)
actual_val.assertEqual(expected_val, msg=prop_name)

# timeDetected and TimeMediaDetected has the same value and SortKey value
# is derived from it
Expand Down
51 changes: 26 additions & 25 deletions src/tests/dbus-tests/udiskstestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _check(self, timeout, check_fn, poll_vg=None):

return False

def assertEqual(self, value, timeout=TIMEOUT, getter=None, poll_vg=None):
def assertEqual(self, value, timeout=TIMEOUT, getter=None, poll_vg=None, msg=None):
if getter is not None:
check_fn = lambda x: getter(x) == value
else:
Expand All @@ -136,11 +136,11 @@ def assertEqual(self, value, timeout=TIMEOUT, getter=None, poll_vg=None):

if not ret:
if getter is not None:
raise AssertionError('%s != %s' % (getter(self._value), value))
raise AssertionError('%s != %s%s' % (getter(self._value), value, ' (%s)' % msg if msg else ''))
else:
raise AssertionError('%s != %s' % (self._value, value))
raise AssertionError('%s != %s%s' % (self._value, value, ' (%s)' % msg if msg else ''))

def assertNotEqual(self, value, timeout=TIMEOUT, getter=None):
def assertNotEqual(self, value, timeout=TIMEOUT, getter=None, msg=None):
if getter is not None:
check_fn = lambda x: getter(x) != value
else:
Expand All @@ -149,9 +149,9 @@ def assertNotEqual(self, value, timeout=TIMEOUT, getter=None):

if not ret:
if getter is not None:
raise AssertionError('%s == %s' % (getter(self._value), value))
raise AssertionError('%s != %s%s' % (getter(self._value), value, ' (%s)' % msg if msg else ''))
else:
raise AssertionError('%s == %s' % (self._value, value))
raise AssertionError('%s != %s%s' % (self._value, value, ' (%s)' % msg if msg else ''))

def assertAlmostEqual(self, value, delta, timeout=TIMEOUT, getter=None):
if getter is not None:
Expand All @@ -168,47 +168,47 @@ def assertAlmostEqual(self, value, delta, timeout=TIMEOUT, getter=None):
raise AssertionError('%s is not almost equal to %s (delta = %s)' % (self._value,
value, delta))

def assertGreater(self, value, timeout=TIMEOUT):
def assertGreater(self, value, timeout=TIMEOUT, msg=None):
check_fn = lambda x: x > value
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('%s is not greater than %s' % (self._value, value))
raise AssertionError('%s is not greater than %s%s' % (self._value, value, ' (%s)' % msg if msg else ''))

def assertLess(self, value, timeout=TIMEOUT):
def assertLess(self, value, timeout=TIMEOUT, msg=None):
check_fn = lambda x: x < value
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('%s is not less than %s' % (self._value, value))
raise AssertionError('%s is not less than %s%s' % (self._value, value, ' (%s)' % msg if msg else ''))

def assertIn(self, lst, timeout=TIMEOUT):
def assertIn(self, lst, timeout=TIMEOUT, msg=None):
check_fn = lambda x: x in lst
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('%s not found in %s' % (self._value, lst))

def assertNotIn(self, lst, timeout=TIMEOUT):
def assertNotIn(self, lst, timeout=TIMEOUT, msg=None):
check_fn = lambda x: x not in lst
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('%s unexpectedly found in %s' % (self._value, lst))
raise AssertionError('%s unexpectedly found in %s%s' % (self._value, lst, ' (%s)' % msg if msg else ''))

def assertTrue(self, timeout=TIMEOUT):
def assertTrue(self, timeout=TIMEOUT, msg=None):
check_fn = lambda x: bool(x)
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('%s is not true' % self._value)
raise AssertionError('%s is not true%s' % (self._value, ' (%s)' % msg if msg else ''))

def assertFalse(self, timeout=TIMEOUT):
def assertFalse(self, timeout=TIMEOUT, msg=None):
check_fn = lambda x: not bool(x)
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('%s is not false' % self._value)
raise AssertionError('%s is not false%s' % (self._value, ' (%s)' % msg if msg else ''))

def assertIsNone(self, timeout=TIMEOUT):
check_fn = lambda x: x is None
Expand All @@ -217,30 +217,31 @@ def assertIsNone(self, timeout=TIMEOUT):
if not ret:
raise AssertionError('%s is not None' % self._value)

def assertIsNotNone(self, timeout=TIMEOUT):
def assertIsNotNone(self, timeout=TIMEOUT, msg=None):
check_fn = lambda x: x is not None
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('unexpectedly None')
raise AssertionError('unexpectedly None%s' % ' (%s)' % msg if msg else '')

def assertLen(self, length, timeout=TIMEOUT):
def assertLen(self, length, timeout=TIMEOUT, msg=None):
check_fn = lambda x: len(x) == length
ret = self._check(timeout, check_fn)

if not ret:
if not hasattr(self._value, '__len__'):
raise AssertionError('%s has no length' % type(self._value))
else:
raise AssertionError('Expected length %d, but %s has length %d' % (length,
self._value,
len(self._value)))
def assertContains(self, member, timeout=TIMEOUT):
raise AssertionError('Expected length %d, but %s has length %d%s' % (length,
self._value,
len(self._value,
' (%s)' % msg if msg else '')))
def assertContains(self, member, timeout=TIMEOUT, msg=None):
check_fn = lambda x: member in x
ret = self._check(timeout, check_fn)

if not ret:
raise AssertionError('%s does not contain %s' % (self._value, member))
raise AssertionError('%s does not contain %s%s' % (self._value, member, ' (%s)' % msg if msg else ''))


class UdisksTestCase(unittest.TestCase):
Expand Down