Reputation: 51
How to run a particular test case multiple times and display the pass and fail count under Test Statistics?
Below is the current code I have to run a test case multiple times. (The test case is implemented in a keyword and called)
*** Test Cases ***
Testcase
repeat keyword 5 Run Keyword And Continue On Failure Execute
*** Keywords ***
Execute
log Hello world!
The code is run from cmd using "pybot testcase.robot"
This code runs the test multiple times but I'm not getting the final pass/fail count in the logs. I need to manually count the pass and fail test case repetitions.
So what modifications should I do to get the data automatically and should be seen in Test Statistics of the log also.
Upvotes: 5
Views: 10536
Reputation: 5026
You can use a small pre-run modifier to dynamically replicate the test cases the requested number of times:
from robot.api import SuiteVisitor
class RunMultiple(SuiteVisitor):
def __init__(self, repeat: int = 2):
self.repeat = repeat
def start_suite(self, suite):
if suite.tests:
tests = list(t for t in suite.tests)
suite.tests.clear()
for tc in tests:
for n in range(self.repeat):
new_tc = suite.tests.create(name=f'{tc.name} Run#{n:03d}')
new_tc.tags = tc.tags
new_tc.setup = tc.setup
new_tc.teardown = tc.teardown
new_tc.template = tc.template
new_tc.body = tc.body
Save this to a file named RunMultiple.py
(same as the class name) in a folder which is on PYTHONPATH
, then add this to your robot command line:
--prerunmodifier RunMultiple:100
to run all test cases 100 times.
(if the prerunmodifier is not on PYTHONPATH
, specify the full path instead).
Explanation: this prerunmodifier will replace the list of test cases in each suite by as many copies of the original test cases as given as parameter.
RF requires each test case to have a unique name. Therefore, the copies are decorated with a unique suffix. Since all copies share the same tags, you can still do statistics by tag, though.
Upvotes: 0
Reputation: 307
Instead of using "Repeat Keyword", use For loop. Use "Run Keyword And Return Status" instead of "Run Keyword And Continue On Failure ".
*** Test Cases ***
Test Me
${fail}= Set Variable 0
:FOR ${index} IN RANGE 5
\ ${passed}= Run Keyword and Return Status Execute
\ Continue For Loop If ${passed}
\ ${fail}= ${fail} + 1
${success}= Set Variable 5 - ${fail}
Log Many Success: ${success}
Log Many fail: ${fail}
Upvotes: 2