无法在使用JUNIT5运行Cucumber Feature文件时进行筛选。

huangapple go评论77阅读模式
英文:

Unable to filter Cucumber Feature files while running them with JUNIT5

问题

我能够使用组合 cucumber+JUnit5+Gradle 并行运行 Cucumber 特性文件。但是无法筛选需要运行的特性文件;

我的特性文件位于资源文件夹下的路径 "com.automation.runners"。而且,@Cucumber 类也位于同名的包下,但在 java 文件夹下;
使用这个配置,我能够运行所有特性文件,但我想只运行一个特性文件。

在 "junit-platform.properties" 中有一个条目 cucumber.features=com/automation/runners/Demo.feature。但这个解决方案对我不起作用。请帮助我!

英文:

I'm able to run cucumber feature files in parallel with combination cucumber+JUnit5+Gradle. But unable to filter feature files which needs to be run;

My feature files are located under resource folder at path "com.automation.runners" . And, @Cucumber class is also located at the package name but under java folder;
With this configuration I am able run all feature files but I want run only one feature file.

I have an entry cucumber.features=com/automation/runners/Demo.feature in "junit-platform.properties". But this solution is not working for me. Please help me!!

答案1

得分: 1

如果您查看Cucumber Junit 5集成的文档,您会发现cucumber.features未列为支持的属性之一。如果您阅读@Cucumber的Java文档

 * 一些构建工具不支持
 * Cucumber使用的{@link org.junit.platform.engine.discovery.DiscoverySelectors}
 * 作为解决方法Cucumber将扫描带有注释的类的包查找功能文件并执行它们

因此,您可以预期将执行所有功能。

要运行单个功能,您可以使用Console Launcher,并使用--select-file选项,或者使用JUnit Platform Launcher API。对于这两者,您可以使用受支持的选择器

package com.example;

import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

public class RunSingleFile {

    public static void main(String... args) {
        Launcher launcher = LauncherFactory.create();
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(DiscoverySelectors.selectFile("path/to/my.feature"))
                .build();
        launcher.execute(request);
    }

}
英文:

If you look at the documentation for the Cucumber Junit 5 integration you'll see that cucumber.features is not listed as one of the supported properties. And if you read the Java doc on @Cucumber:

 * Some build tools do not support the
 * {@link org.junit.platform.engine.discovery.DiscoverySelectors} used by
 * Cucumber. As a work around Cucumber will scan the package of the annotated
 * class for feature files and execute them.

So it is to be expected that all your features are executed.

To run a single feature you can use the Console Launcher with --select-file or the JUnit Platform Launcher API. For both you can use the supported selectors.

package com.example;

import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

public class RunSingleFile {

    public static void main(String... args) {
        Launcher launcher = LauncherFactory.create();
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(DiscoverySelectors.selectFile("path/to/my.feature"))
                .build();
        launcher.execute(request);
    }

}

huangapple
  • 本文由 发表于 2020年9月26日 02:55:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64069939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定