Reputation: 534
Example Python Robot framework test :
*** Keywords ***
Calculate
[Arguments] ${expression} ${expected}
Push buttons C${expression}=
Result should be ${expected}
Calculation should fail
[Arguments] ${expression} ${expected}
${error} = Should cause error C${expression}=
Should be equal ${expected} ${error} # Using `BuiltIn` keyword
Here is the sample behind the scenes code:
class CalculatorLibrary(object):
"""Test library for testing *Calculator* business logic.
Interacts with the calculator directly using its ``push`` method.
"""
**is_skip = False**
def result_should_be(self, expected):
"""Verifies that the current result is ``expected``.
Example:
| Push Buttons | 1 + 2 = |
| Result Should Be | 3 |
"""
if self._result != expected:
**is_skip = True**
**NOW MARK THE TEST AS SKIPPED**
raise AssertionError('%s != %s' % (self._result, expected))
def should_cause_error(self, expression):
"""Verifies that calculating the given ``expression`` causes an error.
The error message is returned and can be verified using, for example,
`Should Be Equal` or other keywords in `BuiltIn` library.
Examples:
| Should Cause Error | invalid | |
| ${error} = | Should Cause Error | 1 / 0 |
| Should Be Equal | ${error} | Division by zero. |
"""
try:
self.push_buttons(expression)
except Exception as err:
return str(err)
else:
raise AssertionError("'%s' should have caused an error."
% expression)
I would like to skip a Robot test at runtime within Python code instead of using the Robot Framework keyword. The use case could be if we would like to skip tests in case a certain component for example calulator is down in the test environment
Upvotes: 0
Views: 752
Reputation: 495
You can use the same keyword inside your Python library, unless there's something more specific preventing that.
To read in more detail you can refer to the Robot Framework User Guide.
In brief, you need to import the Robot Framework library you wish to use normally to the Python script and call the keywords as Python functions. This should work for you:
from robot.libraries.BuiltIn import BuiltIn
class CalculatorLibrary(object):
def result_should_be(self, expected):
if self._result != expected:
BuiltIn().skip(msg='Skipped with Skip keyword.')
Alternatively you could use the skip_if(condition, msg=None)
to remove a need for separate check altogether. Just notice that the skip
keyword skips the whole rest of the test - There won't be any errors raised after the skip call or anything else to that effect.
The link is also in the user guide, but you can find the API documented in a separate location.
Upvotes: 1