有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个maven项目,包含多个模块和一个公共父模块。在这个项目中,有一些与Junit和surefire一起运行的单元测试,以及BDD集成测试。我想运行两个独立的作业,一个运行所有单元测试,另一个运行BDD/集成测试。为了做到这一点,我用Junit类别注释注释了我的BDD runner类,如下所示:

@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时,应该执行用<groups>标记中包含的类别注释的类。实际上,没有一个带注释的类被执行

值得注意的一点是,当我使用maven-surefire-plugin版本2.18时,这是有效的,但从版本2.18.1及其以后就没有了。根据页面底部的documentation,版本2.18.1中关于继承类别的内容发生了更改,但在我的情况下,这些类别正在被继承


共 (1) 个答案

  1. # 1 楼答案

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

    对于我上面描述的特定设置,所有BDD测试都在特定模块中运行,一个解决方法是覆盖子模块中的<groups>配置,如下所示:

     <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