如何在Cucumber Java+JUnit框架中获取Scenario Outline的Examples行数
在Cucumber Java + JUnit框架中获取Scenario Outline的Examples行数
要获取Scenario Outline里Examples部分的数据行数,这里提供两种实用实现方式:
方法一:解析Feature文件统计行数
通过读取Feature文件内容,定位目标Scenario Outline对应的Examples块,手动统计数据行数量:
- 编写Hook类,在场景执行前解析文件:
import io.cucumber.java.Before; import io.cucumber.java.Scenario; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.util.List; public class ExampleRowCounter { private static Integer totalExampleRows; @Before public void calculateExampleRows(Scenario scenario) throws IOException { // 仅首次执行时统计,避免重复计算 if (totalExampleRows == null && scenario.getName().contains("Scenario Outline")) { // 替换为你的Feature文件路径 File featureFile = new File("src/test/resources/features/your_test.feature"); List<String> allLines = FileUtils.readLines(featureFile, "UTF-8"); boolean targetScenarioFound = false; boolean insideExamplesBlock = false; int rowCount = 0; for (String line : allLines) { String trimmedLine = line.trim(); // 定位到目标Scenario Outline if (trimmedLine.startsWith("Scenario Outline:") && trimmedLine.contains(scenario.getName().split("\\|")[0].trim())) { targetScenarioFound = true; } // 进入Examples块 if (targetScenarioFound && trimmedLine.startsWith("Examples:")) { insideExamplesBlock = true; continue; } // 统计数据行(跳过表头) if (insideExamplesBlock && trimmedLine.startsWith("|")) { if (rowCount > 0) { rowCount++; } else { rowCount = 1; // 表头记为第1行,后续数据行累加 } } // 离开Examples块时终止统计 if (insideExamplesBlock && !trimmedLine.startsWith("|") && !trimmedLine.isEmpty()) { break; } } totalExampleRows = rowCount - 1; // 减去表头行,得到实际数据行数 } } // 供Step类调用获取行数 public static Integer getTotalExampleRows() { return totalExampleRows; } }
- 在Step定义中调用方法获取行数:
import io.cucumber.java.en.Then; public class TestSteps { @Then("Step 4") public void executeStep4() { Integer rowCount = ExampleRowCounter.getTotalExampleRows(); System.out.println("Examples数据行数:" + rowCount); // 输出4 } }
方法二:利用Cucumber内部API(推荐)
通过Cucumber的Pickle和Feature模型直接关联到Scenario Outline,获取Examples的表格数据行数:
- 编写Hook类,注入内部对象并统计:
import io.cucumber.java.Before; import io.cucumber.core.gherkin.Pickle; import io.cucumber.core.gherkin.Feature; import io.cucumber.core.gherkin.ScenarioOutline; import java.util.Optional; public class ExampleRowCounter { private static Integer totalExampleRows; @Before public void calculateExampleRows(Pickle pickle, Feature feature) { if (totalExampleRows == null) { // 匹配当前Pickle对应的ScenarioOutline Optional<ScenarioOutline> targetOutline = feature.getChildren().stream() .filter(child -> child instanceof ScenarioOutline) .map(child -> (ScenarioOutline) child) .filter(outline -> outline.getName().equals(pickle.getName().split("\\|")[0].trim())) .findFirst(); if (targetOutline.isPresent()) { // 获取第一个Examples块的数据行数(多Examples块需遍历累加) totalExampleRows = targetOutline.get().getExamples().get(0).getTableBody().size(); } } } public static Integer getTotalExampleRows() { return totalExampleRows; } }
- 同样在Step中调用
ExampleRowCounter.getTotalExampleRows()即可得到目标数值。
注意事项
- 若单个Scenario Outline包含多个Examples块,方法二中需遍历
getExamples()集合并累加各块的行数。 - 方法二使用的Cucumber内部API可能随版本变动,建议使用稳定版本(如7.x系列)。
内容的提问来源于stack exchange,提问作者autoseljava
相关产品推荐
相关产品推荐

