I want to use Robot Framework to write and execute Test Cases in Gherkin format. What I want is when I execute a Test Case, output in the console besides the name of the Scenario, each step (When, Then...) and log as well if the step passes or not.
Asked
Active
Viewed 405 times
1 Answers
0
You could achieve such functionality with a listener that uses the listener interface of the framework.
The end_keyword
listener method will be invoked during execution when a keyword is finished. It will get the keyword name
and its attributes
as a parameter, so you can log both the name and the status.
You have to filter it so only keywords starting with Given
, When
, Then
will be logged on the console.
Example:
ROBOT_LISTENER_API_VERSION = 2
def start_test(name, attributes):
# Add an extra new line at the beginning of each test case to have everything aligned.
print(f'\n')
def end_keyword(name, attributes):
if name.startswith('Given') or name.startswith('When') or name.startswith('Then'):
print(f'{name} | {attributes["status"]} |')
Console output for the behavior-driven development example of the user guide.
robot --pythonpath . --listener listener.py test.robot
==============================================================================
Test
==============================================================================
Add two numbers
Given I have Calculator open | PASS |
.When I add 2 and 40 | PASS |
.Then result should be 42 | PASS |
Add two numbers | PASS |
------------------------------------------------------------------------------
Add negative numbers
Given I have Calculator open | PASS |
.When I add 1 and -2 | PASS |
.Then result should be -1 | PASS |
Add negative numbers | PASS |
------------------------------------------------------------------------------
Test | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================

Bence Kaulics
- 7,066
- 7
- 33
- 63