简体   繁体   English

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

[英]Running Cucumber tests with Junit Categories via Maven

I have a maven project with multiple modules and one common parent module. 我有一个包含多个模块和一个共同父模块的maven项目。 In this project there are some unit tests run with Junit together with surefire, as well as BDD Cucumber integration tests. 在这个项目中,有一些单元测试与Junit以及surefire一起运行,以及BDD Cucumber集成测试。 I would like to run two separate jobs, one to run all the unit tests, and one for running the BDD/Integration tests. 我想运行两个单独的作业,一个用于运行所有单元测试,另一个用于运行BDD / Integration测试。 In order to do that, I have annotated my BDD runner classes with a Junit category annotation like so: 为了做到这一点,我使用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 {
}

I created a Maven profile in the parent pom which uses the maven-surefire-plugin feature that is intended to filter tests base on groups. 我在父pom创建了一个Maven配置文件,它使用maven-surefire-plugin功能,旨在根据组过滤测试。 Here is my maven configuration: 这是我的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>

What I expected was that when I run mvn test -PtestJewels the classes annotated with the category contained in the <groups> tag should be executed. 我所期望的是,当我运行mvn test -PtestJewels ,应该执行使用<groups>标签中包含的类别注释的类。 In actuality none of the annotated classes executed. 实际上,没有任何带注释的类被执行。

An interesting point to note is that when I used the maven-surefire-plugin version 2.18 this worked, but from version 2.18.1 and on it did not. 一个有趣的注意事项是,当我使用maven-surefire-plugin版本2.18时,这是有效的,但是从版本2.18.1开始,它没有。 According to the documentation at the bottom of the page, there was a change in version 2.18.1 regarding categories being inherited but in my case they are being in 根据页面底部的文档 ,版本2.18.1中有关于继承类别的更改,但在我的情况下,它们是在

I discovered that there is actually a pull request on the cucumber-jvm repository to fix this specific problem. 我发现在cucumber-jvm存储库上实际上有一个pull请求来修复这个特定的问题。 The problem is caused as a result of the fact that when Junit filters the test runner classes based on the @Category annotation, it also checks all the child classes as well. 问题是由于当Junit根据@Category注释过滤测试运行器类时,它也会检查所有子类。 In the case of a cucumber test that runs .feature files, all the classes that represent the .feature files are checked as well (cucumber converts each .feature file in an instance of the FeatureRunner class). 在一个运行黄瓜测试的情况下.feature文件,所有表示的类.feature文件被检查,以及(黄瓜将每个.feature在实例文件FeatureRunner类)。 Being that the annotation does not exist on the FeatureRunner class, the tests are filtered out and not run. 由于FeatureRunner类上不存在注释,因此测试将被过滤掉而不会运行。 (Prior to version 2.18.1 the maven-surefire-plugin did not check child classes for annotations and hence this was not a problem. (在版本2.18.1之前, maven-surefire-plugin没有检查子类的注释,因此这不是问题。

A workaround that works for the specific setup that I described above, where all the BDD tests are run in a specific module, was to override the <groups> configuration in the child module, like so: 对于我在上面描述的特定设置(适用于特定模块中运行所有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>

This will obviously not work in a different setup, and it is therefore rather unfortunate that the cucumber team has yet to merge the PR that I mentioned above. 这显然不适用于不同的设置,因此非常不幸的是黄瓜团队尚未合并我上面提到的PR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM