forked from robotframework/robotframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_error.py
More file actions
84 lines (71 loc) · 2.95 KB
/
Copy pathtest_error.py
File metadata and controls
84 lines (71 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import unittest
import sys
import re
from robot.utils.asserts import assert_equal, assert_true, assert_raises
from robot.utils.error import get_error_details, get_error_message, ErrorDetails
class TestGetErrorDetails(unittest.TestCase):
def test_get_error_details_python(self):
for exception, msg, exp_msg in [
(AssertionError, 'My Error', 'My Error'),
(AssertionError, None, 'None'),
(Exception, 'Another Error', 'Another Error'),
(ValueError, 'Something', 'ValueError: Something'),
(AssertionError, 'Msg\nin 3\nlines', 'Msg\nin 3\nlines'),
(ValueError, '2\nlines', 'ValueError: 2\nlines')]:
try:
raise exception(msg)
except:
message, details = get_error_details()
assert_equal(message, get_error_message())
assert_equal(message, exp_msg)
assert_true(details.startswith('Traceback'))
assert_true(exp_msg not in details)
def test_get_error_details_python_class(self):
for exception in [AssertionError, ValueError, ZeroDivisionError]:
try:
raise exception
except:
message, details = get_error_details()
assert_equal(message, get_error_message())
exp_msg = exception.__name__
assert_equal(message, exception.__name__)
assert_true(details.startswith('Traceback'))
assert_true(exp_msg not in details)
class TestRemoveRobotEntriesFromTraceback(unittest.TestCase):
def test_both_robot_and_non_robot_entries(self):
def raises():
raise Exception
self._verify_traceback(r'''
Traceback \(most recent call last\):
File ".*", line \d+, in raises
raise Exception
'''.strip(), assert_raises, AssertionError, raises)
def test_remove_entries_with_lambda_and_multiple_entries(self):
def raises():
1/0
raising_lambda = lambda: raises()
self._verify_traceback(r'''
Traceback \(most recent call last\):
File ".*", line \d+, in <lambda.*>
raising_lambda = lambda: raises\(\)
File ".*", line \d+, in raises
1/0
'''.strip(), assert_raises, AssertionError, raising_lambda)
def test_only_robot_entries(self):
self._verify_traceback(r'''
Traceback \(most recent call last\):
None
'''.strip(), assert_equal, 1, 2)
def _verify_traceback(self, expected, method, *args):
try:
method(*args)
except Exception:
type, value, tb = sys.exc_info()
# first tb entry originates from this file and must be excluded
traceback = ErrorDetails((type, value, tb.tb_next)).traceback
else:
raise AssertionError
if not re.match(expected, traceback):
raise AssertionError('\nExpected:\n%s\n\nActual:\n%s' % (expected, traceback))
if __name__ == "__main__":
unittest.main()