Improve failure message on test failure.
@@ -27,6 +27,18 @@ def assert_test(test, name, status, tags=(), msg=''):
class TestImports(unittest.TestCase):
+ def run_and_check_pass(self, suite):
+ result = run(suite)
+ try:
+ assert_suite(result, 'Suite', 'PASS')
+ assert_test(result.tests[0], 'Test', 'PASS')
+ except AssertionError as e:
+ # Something failed. Let's print more info.
+ full_msg = ["Expected and obtained don't match. Test messages:"]
+ for test in result.tests:
+ full_msg.append('%s: %s' % (test, test.message))
+ raise AssertionError('\n'.join(full_msg)) from e
+
def test_create(self):
suite = TestSuite(name='Suite')
suite.resource.imports.create('Library', 'OperatingSystem')
@@ -36,27 +48,22 @@ class TestImports(unittest.TestCase):
test.body.create_keyword('Directory Should Exist', args=['.'])
test.body.create_keyword('My Test Keyword')
test.body.create_keyword('Convert To Lower Case', args=['ROBOT'])
- result = run(suite)
- assert_suite(result, 'Suite', 'PASS')
- assert_test(result.tests[0], 'Test', 'PASS')
+ self.run_and_check_pass(suite)
+
def test_library(self):
suite = TestSuite(name='Suite')
suite.resource.imports.library('OperatingSystem')
suite.tests.create(name='Test').body.create_keyword('Directory Should Exist',
args=['.'])
- result = run(suite)
- assert_suite(result, 'Suite', 'PASS')
- assert_test(result.tests[0], 'Test', 'PASS')
+ self.run_and_check_pass(suite)
def test_resource(self):
suite = TestSuite(name='Suite')
suite.resource.imports.resource('test_resource.txt')
suite.tests.create(name='Test').body.create_keyword('My Test Keyword')
assert_equal(suite.tests[0].body[0].name, 'My Test Keyword')
- result = run(suite)
- assert_suite(result, 'Suite', 'PASS')
- assert_test(result.tests[0], 'Test', 'PASS')
+ self.run_and_check_pass(suite)
def test_variables(self):
suite = TestSuite(name='Suite')
@@ -65,9 +72,7 @@ class TestImports(unittest.TestCase):
'Should Be Equal As Strings',
args=['${MY_VARIABLE}', 'An example string']
)
- result = run(suite)
- assert_suite(result, 'Suite', 'PASS')
- assert_test(result.tests[0], 'Test', 'PASS')
+ self.run_and_check_pass(suite)
def test_invalid_type(self):
assert_raises_with_msg(ValueError,
