简体   繁体   English

使用 Apache Maven Surefire 在 Junit 5 平台上运行基于文件的测试

[英]Running file-based tests on Junit 5 Platform using Apache Maven Surefire

I have implemented a custom TestEngine class that is able to discover and run unit tests from *.csv files (not java classes) by checking for FileSelector in the EngineDiscoveryRequest from Junit 5. I have implemented a custom TestEngine class that is able to discover and run unit tests from *.csv files (not java classes) by checking for FileSelector in the EngineDiscoveryRequest from Junit 5.

I am able to run these tests without problems using the ConsoleLauncher provided by the Junit team, by invoking org.junit.platform.console.ConsoleLauncher -f build/resources/test/testfile_1.csv (this is invoked from a gradle task because gradle does not support resource-based testing but that should not matter here). I am able to run these tests without problems using the ConsoleLauncher provided by the Junit team, by invoking org.junit.platform.console.ConsoleLauncher -f build/resources/test/testfile_1.csv (this is invoked from a gradle task because gradle不支持基于资源的测试,但这在这里不重要)。

Now, I am trying to run the same tests using the same custom test engine but by using maven and the surefire plugin instead of the console launcher.现在,我尝试使用相同的自定义测试引擎运行相同的测试,但使用 maven 和 surefire 插件而不是控制台启动器。 My current pom.xml looks as follows (relevant part):我当前的pom.xml看起来如下(相关部分):

<project>
    <dependencies>
        <dependency>
            <groupId>my_custom_test_engine</groupId>
            <artifactId>custom_test_engine</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <version>1.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-console</artifactId>
            <version>1.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.30</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
        </plugins>
    </build>
</project>

Unfortunately, surefire:test does not appear to even call my test engine for discovery and just finishes with:不幸的是, surefire:test似乎甚至没有调用我的测试引擎进行发现,只是完成了:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.626 s
[INFO] Finished at: 2020-06-20T17:00:01+02:00
[INFO] ------------------------------------------------------------------------

I do not have any test classes in my src folder, just one test file under resources/testfile_1.csv .我的src文件夹中没有任何测试类,只有resources/testfile_1.csv下的一个测试文件。

Does surefire even support file-based testing with Junit 5? Surefire 是否甚至支持使用 Junit 5 进行基于文件的测试? If so, how can I force Surefire to call my custom test engine for test discovery?如果是这样,我如何强制 Surefire 调用我的自定义测试引擎进行测试发现?

-- Update - 更新

The discovery part of my TestEngine implementation looks something like this (only relevant part shown):我的TestEngine实现的发现部分看起来像这样(仅显示相关部分):

public class MyTestEngine implements TestEngine {

    @Override
    public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
        EngineDescriptor engineDescriptor = new EngineDescriptor(uniqueId, name);

        logger.debug("Discovering tests");

        discoveryRequest.getSelectorsByType(FileSelector.class).forEach(selector -> {
            logger.debug("Discovered: {}", selector.getFile().getAbsolutePath());
                engineDescriptor.addChild(new TestDescriptor (engineDescriptor.getUniqueId(), selector.getFile().getName()));

        });
}

-- Update 2 My test files are located under src/test/resources . -- 更新 2 我的测试文件位于src/test/resources下。

Figured it out.弄清楚了。 Credit goes to @johanneslink for pointing me in the right direction.感谢@johanneslink 为我指明了正确的方向。

Surefire indeed does not support resource-based testing (see also this issue ). Surefire 确实不支持基于资源的测试(另请参阅此问题)。 What I did instead (as @johanneslink advised), was to create an empty class, which can be detected by surefire so that the custom TestEngine is invoked:相反,我所做的(正如@johanneslink 建议的那样)是创建一个空的 class,可以通过surefire 检测到,以便调用自定义的TestEngine

public class TestInitializer {
}

Then, in my TestEngine , I check for ClassSelector and discover the resource-based tests:然后,在我的TestEngine中,我检查ClassSelector并发现基于资源的测试:

discoveryRequest.getSelectorsByType(ClassSelector.class).forEach(selector -> {
            if (selector.getClassName().equals(TestInitializer.class.getName())) {
                discoverTests(new File(System.getenv("test_dir")), engineDescriptor);
            }
        });

The actual directory where my test files are located is specified with the environment variable test_dir .我的测试文件所在的实际目录由环境变量test_dir指定。

Then, for actually using the custom TestEngine , modify your pom.xml as follows:然后,为了实际使用自定义TestEngine ,修改你的pom.xml如下:

   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <dependenciesToScan>
                        <dependency>
                            my_custom_test_engine:custom_test_engine
                        </dependency>
                    </dependenciesToScan>
                    <includes>
                        <include>**/TestInitializer*
                        </include>
                    </includes>
                    <environmentVariables>
                        <test_dir>target/test-classes
                        </test_dir>
                    </environmentVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>

The dependenciesToScan property will ensure, that surefire scans the dependency containing the TestInitializer class. dependenciesToScan属性将确保,surefire 扫描包含TestInitializer class 的依赖项。

Note that the latest version 3.0.0-M5 of maven-surefire-plugin appears to be broken in this regard as the tests will be discovered and run, but surefire will not pick up on the results.请注意, maven-surefire-plugin的最新版本3.0.0-M5在这方面似乎被破坏了,因为测试将被发现并运行,但 Surefire 不会收到结果。 Use 2.22.2 instead.请改用2.22.2

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

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