我正在使用JUnit Jupiter 5.0.0版(发行版),并且试图使用测试发现功能。
Junit的文档可以在7.1.1中找到。从http://junit.org/junit5/docs/5.0.0/user-guide/#launcher-api-discovery发现测试

我的实现是:

import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;

import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.LoggingListener;

public class MainPrueba {

    public static void main(String[] args) throws InterruptedException {

        Runnable task = () -> {
            System.out.println("Runing thread INI");

            LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(
                        selectPackage("org.package.qabootfx.test.ping")
                        //,selectClass(QabootfxApplicationTests.class)
                )
                .filters(
                    //includeClassNamePatterns(".*Test")
                        includeClassNamePatterns(".*")
                )
                .build();

            Launcher launcher = LauncherFactory.create();

            TestPlan testPlan = launcher.discover(request);

            for (TestIdentifier root : testPlan.getRoots()) {
                System.out.println("Root: " + root.toString());

                for (TestIdentifier test : testPlan.getChildren(root)) {
                    System.out.println("Found test: " + test.toString());
                }
            }

            // Register a listener of your choice
            //TestExecutionListener listener = new SummaryGeneratingListener();
            TestExecutionListener listener = LoggingListener.forJavaUtilLogging(); //new LoggingListener();
            launcher.registerTestExecutionListeners(listener);


            launcher.execute(request);

            System.out.println("Runing thread END");

        };
        new Thread(task).start();

        Thread.sleep(5000);
        System.out.println("END");
    }

}


检查LoggingListener类的实现,我们可以看到这必须将结果打印到控制台。例如:

package org.junit.platform.launcher.listeners;

@API(status = MAINTAINED, since = "1.0")
public class LoggingListener implements TestExecutionListener {

    ....

    @Override
    public void testPlanExecutionStarted(TestPlan testPlan) {
        log("TestPlan Execution Started: %s", testPlan);
    }

    @Override
    public void testPlanExecutionFinished(TestPlan testPlan) {
        log("TestPlan Execution Finished: %s", testPlan);
    }

    ...

}


我的测试课是:

public class QabootfxApplicationTest {

    @Test
    public void testAbout() {
        System.out.println("TEST Execution....  QabootfxApplicationTests.testAbout()");

        assertEquals(4, 5, "The optional assertion message is now the last parameter.");
    }
}


我期望在控制台中看到类似以下内容:

2017-09-20 10:53:48.041  INFO 11596 --- TestPlan Execution Started: ....
2017-09-20 10:53:48.041  INFO 11596 --- TestPlan Execution Finished: ....


但是我看不到类似于“ ... TestPlan Execution Started ...”的内容。

控制台输出为:

Runing thread INI
Root: TestIdentifier [uniqueId = '[engine:junit-jupiter]', parentId = null, displayName = 'JUnit Jupiter', legacyReportingName = 'JUnit Jupiter', source = null, tags = [], type = CONTAINER]
Found test: TestIdentifier [uniqueId = '[engine:junit-jupiter]/[class:org.package.qabootfx.test.ping.QabootfxApplicationTest]', parentId = '[engine:junit-jupiter]', displayName = 'QabootfxApplicationTest', legacyReportingName = 'org.package.qabootfx.test.ping.QabootfxApplicationTest', source = ClassSource [className = 'org.package.qabootfx.test.ping.QabootfxApplicationTest', filePosition = null], tags = [], type = CONTAINER]
TEST Executon....  QabootfxApplicationTests.testAbout()
Runing thread END
END


可能是错误吗?还是我执行错误?

最佳答案

当文档明确声明以下内容时,为什么还要期望LoggingListener.forJavaUtilLogging()创建的侦听器在日志级别INFO ...上记录任何内容?


  创建一个LoggingListener,它使用java.util.logging.Logger的日志级别委派给FINE


如果要LoggingListener在级别INFO上记录消息,则必须使用另一个工厂方法来创建它,该方法接受像LoggingListener.forJavaUtilLogging(Level.INFO)这样的日志级别。

关于java - 使用JUnit 5发现测试不会执行LoggingListener(TestExecutionListener的实现),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46329888/

10-12 00:10