【问题标题】:Running Cucumber tests with Junit Categories via Maven通过 Maven 使用 Junit 类别运行 Cucumber 测试
【发布时间】:2017-01-23 21:38:36
【问题描述】:

我有一个包含多个模块和一个公共父模块的 Maven 项目。在这个项目中,有一些单元测试使用 Junit 和 surefire 一起运行,以及 BDD Cucumber 集成测试。我想运行两个单独的作业,一个运行所有单元测试,一个运行 BDD/集成测试。为此,我使用 Junit 类别注释对我的 BDD 运行器类进行了注释,如下所示:

@RunWith(Cucumber.class)
@CucumberOptions(
                tags = { "@ATagToBeRun", "~@ATagNotToBeRun","~@ToBeImplemented" },
                dryRun = false, strict = true, 
                features = "src/test/resources/cucumber/testing",
                glue = { "com.some.company.test",
                        "com.some.company.another.test"})
@Category(value = IntegrationTest.class)
public class FlowExecutionPojoTest {
}

我在父 pom 中创建了一个 Maven 配置文件,它使用了旨在基于组过滤测试的 maven-surefire-plugin 功能。这是我的 Maven 配置:

     <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-jvm-deps</artifactId>
            <version>1.0.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profile>
        <id>testJewels</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <modules>
            <module>../my-module</module>
        </modules>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <groups>com.some.company.IntegrationTest</groups>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

我的预期是,当我运行mvn test -PtestJewels 时,应该执行使用&lt;groups&gt; 标记中包含的类别注释的类。实际上没有执行任何带注释的类。

需要注意的一个有趣点是,当我使用 maven-surefire-plugin 2.18 版时,它可以工作,但从 2.18.1 版开始并没有。根据页面底部的documentation,版本 2.18.1 中关于被继承的类别发生了变化,但在我的情况下,它们在

【问题讨论】:

  • 你能分享你的 pom 依赖吗?是否包含 cucumber-java、cucumber-junit 包?
  • @eg04lt3r 确定,将其添加到问题中

标签: java junit cucumber maven-surefire-plugin


【解决方案1】:

我发现cucumber-jvm 存储库上实际上有一个pull request 来解决这个特定问题。这个问题是由于当 Junit 根据@Category 注解过滤测试运行器类时,它也会检查所有子类。在运行.feature 文件的黄瓜测试的情况下,代表.feature 文件的所有类也会被检查(黄瓜在FeatureRunner 类的实例中转换每个.feature 文件)。由于 FeatureRunner 类上不存在注释,因此测试被过滤掉并且不运行。 (在 2.18.1 版本之前,maven-surefire-plugin 没有检查子类的注释,因此这不是问题。

适用于我上面描述的特定设置(所有 BDD 测试都在特定模块中运行)的解决方法是覆盖子模块中的 &lt;groups&gt; 配置,如下所示:

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <groups combine.self="override"></groups>
            </configuration>
        </plugin>
    </plugins>
</build>

这显然不会在不同的设置中起作用,因此很遗憾黄瓜团队尚未合并我上面提到的 PR。

【讨论】:

    猜你喜欢
    • 2020-02-06
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    相关资源
    最近更新 更多