为黄瓜的每一步都指定一个Id



我正在寻找在cucumber中对步骤进行分组的方法,是否可以为每个步骤附加一个id?

PickleStepTestStep在运行时已经附加了一个唯一的ID。一种方法是通过如下插件

import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.event.*;

import java.net.URI;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

public class    ReportPlugin implements EventListener {

private final Map<String, UUID> startedSteps = new TreeMap<String, UUID>();
private final Map<String, Status> finishedCases = new TreeMap<String, Status>();

@Override
public void setEventPublisher(EventPublisher publisher) {

publisher.registerHandlerFor(TestStepStarted.class, this::handleTestStepStarted);
publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);
}

private void handleTestStepStarted(TestStepStarted event) {
startedSteps.put(event.getTestStep().toString(), event.getTestStep().getId());
for (Map.Entry<String, UUID> entry : startedSteps.entrySet()) {
String location = entry.getKey();
UUID uuid = entry.getValue();
System.out.println(location + " ###fromTestStepStarted### " + uuid);

//above prints
//io.cucumber.core.runner.PickleStepTestStep@5a5c128 ###fromTestStepStarted### 7f964f1c-9442-43fc-97e9-9ec6717eb47f
// io.cucumber.core.runner.PickleStepTestStep@77b919a3 ###fromTestStepStarted### a5d57753-aecb-40a0-a0cf-76bef7526dd8
}
}
//If you would like to get each test step text you do this
private void handleTestCaseFinished(TestCaseFinished event) {


TestCase testCase = event.getTestCase();
String scenarioName = testCase.getName();
TestStep testStep = testCase.getTestSteps().get(0);
if (testStep instanceof PickleStepTestStep) {
PickleStepTestStep pickleStepTestStep  = (PickleStepTestStep) testStep;
String text = pickleStepTestStep.getStep().getText();
System.out.println("****Pickle Step TestStep*****"+  text);
//above prints
//****Pickle Step TestStep*****I open the site ""  
}

}
}

要运行上面的类,请将该类与您的步骤defs或支持类放在一起,然后在junit-platform.properties(对于Junit5(中提及类似的插件

cucumber.plugin = com.test.support.ReportPlugin

对于Junit4,您可能需要在跑步类中添加插件

当你运行测试时,你应该看到所有打印在控制台上的东西

最新更新