Fix assert message to have a single stacktrace (#565)
* Remove the duplicated stacktrace in asserts.assert_equal.
This change also converts the except statement to catch AssertionError,
which is the py2/3 compatible name for errors raised by both pyunit
versions.
diff --git a/mobly/asserts.py b/mobly/asserts.py
index ae119e5..8716f93 100644
--- a/mobly/asserts.py
+++ b/mobly/asserts.py
@@ -43,18 +43,18 @@
extras: An optional field for extra information to be included in
test result.
"""
+ my_msg = None
try:
_pyunit_proxy.assertEqual(first, second)
- except Exception as e:
- # We have to catch all here for py2/py3 compatibility.
- # In py2, assertEqual throws exceptions.AssertionError, which does not
- # exist in py3. In py3, it throws unittest.case.failureException, which
- # does not exist in py2. To accommodate using explicit catch complicates
- # the code like hell, so I opted to catch all instead.
+ except AssertionError as e:
my_msg = str(e)
if msg:
my_msg = "%s %s" % (my_msg, msg)
- fail(my_msg, extras=extras)
+
+ # This raise statement is outside of the above except statement to prevent
+ # Python3's exception message from having two tracebacks.
+ if my_msg is not None:
+ raise signals.TestFailure(my_msg, extras=extras)
def assert_raises(expected_exception, extras=None, *args, **kwargs):