如何在Robot Framework中多次执行测试用例并单独展示执行状态
Great question! The problem with using a for loop inside a single test case is that Robot treats the entire loop as part of one test entry—so even if some iterations fail, the report only shows the overall pass/fail status of that single test. Here are a couple of practical solutions to get separate statuses for each execution in your report:
Solution 1: Use Test Templates (Best for Static Iterations)
Robot's built-in Test Template feature is perfect for this scenario. It lets you define a reusable keyword as a template, then each row under your test case becomes a separate execution—each with its own entry in the report.
Here's an example:
*** Settings *** Test Template Execute My Reusable Test *** Test Cases *** | Run Test Multiple Times | | Iteration 1 Data | | Iteration 2 Data | | Iteration 3 Data *** Keywords *** Execute My Reusable Test [Arguments] ${test_data} # Your test logic here—e.g., validate UI, API call, etc. Log Running test with data: ${test_data} # Add your assertions (e.g., Page Should Contain, Should Be Equal) Run Keyword And Return Status Should Be True ${test_data} != "Invalid Data"
Each line under Run Test Multiple Times will appear as a separate test in your report, with its own pass/fail status. This works great when you know the number of iterations upfront.
Solution 2: Generate Dynamic Test Cases (Best for Dynamic Data)
If you need to run the test with data that's loaded dynamically (e.g., from a CSV, database, or API), you can generate test cases on the fly during suite setup. This uses Robot's Create Test Case keyword (available in version 3.2+).
Example implementation:
*** Settings *** Suite Setup Generate Dynamic Tests *** Variables *** # Replace with your dynamic data source (e.g., from a file) @{ITERATION_DATA} Valid Input Invalid Input Edge Case Input *** Test Cases *** # Empty initially—test cases will be added dynamically *** Keywords *** Generate Dynamic Tests :FOR ${data} IN @{ITERATION_DATA} \ ${test_name} = Set Variable Test Execution: ${data} \ Create Test Case ${test_name} Execute My Reusable Test ${data} Execute My Reusable Test [Arguments] ${test_data} # Your test logic here Log Executing test with data: ${test_data} Should Be True ${test_data} != "Invalid Input"
This will create a separate test case for each item in your ITERATION_DATA list, each showing up individually in the report with its own status.
Why Your Original For Loop Didn't Work
When you put a for loop inside a single test case, Robot considers the entire loop as one test execution. Even if you use Run Keyword And Continue On Failure, it just keeps running the loop—but the report will only show whether the entire test (loop included) passed or failed. The above solutions avoid this by breaking each iteration into its own test entry.
内容的提问来源于stack exchange,提问作者Qi Huaping




