有没有办法在黄瓜JVM的@BeforeClass注释中获取运行的场景列表

Is there a way to get the list of scenarios that are to be run in the @BeforeClass annotation in cucumber JVM

我有一个需求,需要根据我在 cucumber Test 运行ner 中提供的标签获取要执行的所有场景的列表。但是我必须在测试开始执行之前得到这个列表。

我知道有一个名为“@BeforeClass”的标签,但我不确定我是否可以使用它来获取将要 运行 的所有场景的列表。例如像这样的

 @BeforeClass
public void intialize(Scenario[] scenario) throws Exception { }

下面是我测试的代码 运行ner class

    package com.automation.cucumber;

import com.automation.Utils;
import io.cucumber.java.Scenario;
import io.cucumber.testng.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;

import java.io.File;

@CucumberOptions(features = "features/amazon"
        ,glue="com.automation.cucumber"
        ,tags = "@tt"
        ,dryRun = true
        , plugin = {"json:target/cucumber-reports/cucumber.json"})

public class CucumberTestRunner extends AbstractTestNGCucumberTests {

    static String resultFolder;
    @DataProvider(parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();
    }

    @BeforeClass
    public void intialize() throws Exception {
        resultFolder =  Utils.createTestReportFolder();
        if(resultFolder==null)
        {
            throw new Exception("Unable to create a result folder");
        }

        System.out.println();

    }

}

您可能必须实施 EventListener class 才能获取该信息,并在 Runner class 中执行 dryRun = true @CucumberOptions

引用 question 可以帮助您实现所需的

public class DryRunPlugin implements EventListener {

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestCaseStarted.class, this::handleCaseStarted);
    }

    private void handleCaseStarted(TestCaseStarted event) {
        System.out.println(event.getTestCase().getUri());
        System.out.println(event.getTestCase().getName());
        System.out.println(event.getTestCase().getScenarioDesignation());
        event.getTestCase().getTags().stream().forEach(t -> 
        System.out.println(t.getName()));
    }

}