提问者:小点点

通过Maven使用Junit类别运行cucumber测试


我有一个有多个模块和一个公共父模块的maven项目。在这个项目中,有一些单元测试和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时,使用中包含的类别注释的类

值得注意的一点是,当我使用maven-surefire-plugin版本2.18时,这是有效的,但从版本2.18.1开始,它就没有了。根据页面底部的留档,版本2.18.1中关于继承的类别发生了变化,但在我的情况下,它们是在


共1个答案

匿名用户

我发现实际上cucumber-jvm存储库上有一个拉取请求来修复这个特定的问题。这个问题是由于以下事实造成的:当Junit根据@类别注释过滤测试运行器类时,它也会检查所有的子类。在运行. feature文件的cucumber测试的情况下,所有代表.feature文件的类也会被检查(cucumber在FeatureRunner类的实例中转换每个.feature文件)。由于FeatureRunner类上不存在注释,因此测试被过滤掉而不运行。(在版本2.18.1之前,maven-surefire-plugin没有检查子类的注释,因此这不是问题。

适用于我上面描述的特定设置的解决方法是覆盖,其中所有BDD测试都在特定模块中运行

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

这显然不会在不同的设置中起作用,因此cucumber团队尚未合并我上面提到的PR是相当不幸的。