1

我需要报告正在执行的场景的功能描述以报告给其他系统。能够从 cucumber.api.Scenario 中获取场景名称;我如何才能描述功能?有没有我可以使用的界面?

使用cucumber-Jvm,获取特征描述运行时;因为正在执行的每个场景可能来自不同的功能文件。

4

1 回答 1

2

您可以通过从以下位置检索 Gherkin 功能来获取功能的描述CucumberFeature

List<CucumberFeature> cucumberFeatures = new ArrayList<>();
FeatureBuilder featureBuilder = new FeatureBuilder(cucumberFeatures);

featureBuilder.parse(new FileResource(featureFile.getParentFile(), featureFile), new ArrayList());
for (CucumberFeature feature: cucumberFeatures) {   
    // Here we retrieve the Gherkin model        
    Feature f = feature.getGherkinFeature();

    // Here we get name and description of the feature.
    System.out.format("%s: %s%n", f.getName(), f.getDescription());
}

另一种解决方案是实现自己的formatter,并直接使用 Gherkin 进行解析:

public class MyFormatter implements Formatter {

    private List<Feature> features = new ArrayList<>();

    public static void main(String... args) throws Exception {

            OutputStreamWriter out = new OutputStreamWriter(System.out, "UTF-8");

            // Read the feature file into a string.
            File f = new File("/path/to/file.feature");
            String input = FixJava.readReader(new FileReader(f));

            // Parse the gherkin string with our own formatter.
            MyFormatter formatter = new MyFormatter();
            Parser parser = new Parser(formatter);
            parser.parse(input, f.getPath(), 0);

            for (Feature feature: formatter.features) {
                System.out.format("%s: %s%n", feature.getName(), feature.getDescription());
            }
    }

    @Override
    public void feature(Feature feature) {
        features.add(feature);
    }

    // ...
    // follow all the Formatter methods to implement.
}
于 2015-10-03T08:04:57.997 回答