使用JUnit 5发现测试不会执行LoggingListener(TestExecutionListener的实现) [英] Discovering test with JUnit 5 doesn't execute LoggingListener (implementation of TestExecutionListeners)

查看:214
本文介绍了使用JUnit 5发现测试不会执行LoggingListener(TestExecutionListener的实现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using JUnit Jupiter version 5.0.0 (Release version) and I'm trying to use the test discovery feature. The documentation of Junit can be found in 7.1.1. Discovering Tests from 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类的实现,我们可以看到这必须将结果打印到控制台.例如:

Examining LoggingListener class implementation we can see that this must print to the console the results. For example:

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.");
    }
}

我希望在控制台中看到类似以下内容的东西:

I'm expecting see in the console something similar to:

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 ..."的内容.

but I can't see nothing similar to "... 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 ...上记录任何内容?

Why would you expect the listener created by LoggingListener.forJavaUtilLogging() to log anything at log level INFO... when the documentation explicitly states the following?

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

Create a LoggingListener which delegates to a java.util.logging.Logger using a log level of FINE.

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

If you want the LoggingListener to log messages at level INFO, you'll have to create it using the other factory method which accepts a log level like this LoggingListener.forJavaUtilLogging(Level.INFO).

这篇关于使用JUnit 5发现测试不会执行LoggingListener(TestExecutionListener的实现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆