Skip to content

Commit eed154f

Browse files
committed
Add HTML support to exceptions exposed via robot.api.
Exposing these exceptions was proposed by robotframework#3685 and this enhancement can be considered to be part of that. Fixes robotframework#3866 that explicitly asked to add HTML support to these excpetions. A functional change caused by this change is that these exceptions now require a message. That means, for example, that raise SkipExecution doesn't work anymore and you need to use raise SkipExecution('Some message') instead. I consider that good but if someone has use cases for raising these exceptions without a message changing this can be still changed.
1 parent 1e50ba3 commit eed154f

File tree

7 files changed

+59
-15
lines changed

7 files changed

+59
-15
lines changed

atest/robot/running/skip.robot

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ Suite Setup Run Tests --skip skip-this --SkipOnFailure skip-on-failure --no
33
Resource atest_resource.robot
44

55
*** Test Cases ***
6-
Skip Keyword
6+
Skip keyword
77
Check Test Case ${TEST NAME}
88

9-
Skip with Library Keyword
9+
Skip with SkipExecution exception in library
10+
Check Test Case ${TEST NAME}
11+
12+
Skip with SkipExecution exception in library using HTML
1013
Check Test Case ${TEST NAME}
1114

1215
Skip with custom exception

atest/robot/running/test_case_status.robot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,11 @@ Test Setup And Teardown Fails
5454
robot.api.Failure
5555
Check Test Case ${TEST NAME}
5656

57+
robot.api.Failure with HTML message
58+
Check Test Case ${TEST NAME}
59+
5760
robot.api.Error
5861
Check Test Case ${TEST NAME}
62+
63+
robot.api.Error with HTML message
64+
Check Test Case ${TEST NAME}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from robot.api import Failure, Error
22

33

4-
def failure(msg='I failed my duties'):
5-
raise Failure(msg)
4+
def failure(msg='I failed my duties', html=False):
5+
raise Failure(msg, html)
66

77

8-
def error(msg='I errored my duties'):
9-
raise Error(msg)
8+
def error(msg='I errored my duties', html=False):
9+
raise Error(msg, html=html)

atest/testdata/running/skip/skip.robot

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ Library skiplib.py
55
${TEST_OR_TASK} Test
66

77
*** Test Cases ***
8-
Skip Keyword
8+
Skip keyword
99
[Documentation] SKIP Skipped with Skip keyword.
1010
Skip
1111
Fail Should not be executed!
1212

13-
Skip with Library Keyword
13+
Skip with SkipExecution exception in library
1414
[Documentation] SKIP Show must not got on
1515
Skip with Message Show must not got on
1616
Fail Should not be executed!
1717

18+
Skip with SkipExecution exception in library using HTML
19+
[Documentation] SKIP *HTML* Show <b>must</b> not got on
20+
Skip with Message Show <b>must</b> not got on html=True
21+
Fail Should not be executed!
22+
1823
Skip with custom exception
1924
[Documentation] SKIP CustomSkipException: Skipped with custom exception.
2025
Skip with custom exception

atest/testdata/running/skip/skiplib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ class CustomSkipException(Exception):
55
ROBOT_SKIP_EXECUTION = True
66

77

8-
def skip_with_message(msg):
9-
raise SkipExecution(msg)
8+
def skip_with_message(msg, html=False):
9+
raise SkipExecution(msg, html)
1010

1111

1212
def skip_with_custom_exception():

atest/testdata/running/test_case_status.robot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,18 @@ robot.api.Failure
8383
[Documentation] FAIL I failed my duties
8484
Failure
8585

86+
robot.api.Failure with HTML message
87+
[Documentation] FAIL *HTML* <b>BANG!</b>
88+
Failure <b>BANG!</b> True
89+
8690
robot.api.Error
8791
[Documentation] FAIL I errored my duties
8892
Error
8993

94+
robot.api.Error with HTML message
95+
[Documentation] FAIL *HTML* <b>BANG!</b>
96+
Error <b>BANG!</b> True
97+
9098
*** Keyword ***
9199
Do Nothing
92100
No operation

src/robot/api/exceptions.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ class Failure(AssertionError):
2626
"""Report failed validation.
2727
2828
There is no practical difference in using this exception compared to using
29-
the standard ``AssertionError``. The main benefit of using this exception is
30-
that its name is consistent with other exceptions in this module.
29+
the standard ``AssertionError``. The main benefits are HTML support and that
30+
the name of this exception is consistent with other exceptions in this module.
3131
"""
3232
ROBOT_SUPPRESS_NAME = True
3333

34+
def __init__(self, message, html=False):
35+
"""
36+
:param message: Exception message.
37+
:param html: When ``True``, message is considered to be HTML and not escaped.
38+
"""
39+
AssertionError.__init__(self, message if not html else '*HTML* ' + message)
40+
3441

3542
class ContinuableFailure(Failure):
3643
"""Report failed validation but allow continuing execution."""
@@ -43,12 +50,20 @@ class Error(RuntimeError):
4350
Failures related to the system not behaving as expected should typically be
4451
reported using the :class:`Failure` exception or the standard ``AssertionError``.
4552
This exception can be used, for example, if the keyword is used incorrectly.
46-
There is no practical difference, other than consistent naming with other
47-
exceptions in this module, compared to using this exception and the standard
48-
``RuntimeError``.
53+
54+
There is no practical difference in using this exception compared to using
55+
the standard ``RuntimeError``. The main benefits are HTML support and that
56+
the name of this exception is consistent with other exceptions in this module.
4957
"""
5058
ROBOT_SUPPRESS_NAME = True
5159

60+
def __init__(self, message, html=False):
61+
"""
62+
:param message: Exception message.
63+
:param html: When ``True``, message is considered to be HTML and not escaped.
64+
"""
65+
RuntimeError.__init__(self, message if not html else '*HTML* ' + message)
66+
5267

5368
class FatalError(Error):
5469
"""Report error that stops the whole execution."""
@@ -60,3 +75,10 @@ class SkipExecution(Exception):
6075
"""Mark the executed test or task skipped."""
6176
ROBOT_SKIP_EXECUTION = True
6277
ROBOT_SUPPRESS_NAME = True
78+
79+
def __init__(self, message, html=False):
80+
"""
81+
:param message: Exception message.
82+
:param html: When ``True``, message is considered to be HTML and not escaped.
83+
"""
84+
Exception.__init__(self, message if not html else '*HTML* ' + message)

0 commit comments

Comments
 (0)