Junit5/Jupiter – 在 Maven 构建期间打印方法名称

huangapple go评论75阅读模式
英文:

Junit5/Jupiter - Print method names during maven build for

问题

By default all class names of the unit tests are printed on the console. However I would like to include all methods/individual tests within the class to be printed on the console output when running maven verify.

My current output looks like this:
Junit5/Jupiter – 在 Maven 构建期间打印方法名称

As you can see the class KeyStoreUtilsShould contains 35 individual tests which I would like to be printed one by one. It would also nice to have to see the execution time, which failed and which passed. The reason why I would like to see the individual tests is to easily inspect which tests are already included without digging the source code.

To reproduce:

英文:

By default all class names of the unit tests are printed on the console. However I would like to include all methods/individual tests within the class to be printed on the console output when running maven verify.

My current output looks like this:
Junit5/Jupiter – 在 Maven 构建期间打印方法名称

As you can see the class KeyStoreUtilsShould contains 35 individual tests which I would like to be printed one by one. It would also nice to have to see the execution time, which failed and which passed. The reason why I would like to see the individual tests is to easily inspect which tests are already included without digging the source code.

To reproduce:

答案1

得分: 1

在评论部分,khmarbaise提供了一个链接,对我非常有帮助。我请求他将其提升为答案,而不是评论,以便我可以将其标记为已解决,但他没有这样做,所以我在这里发布了他的答案。

我使用了https://github.com/fabriciorby/maven-surefire-junit5-tree-reporter插件来生成更详细的输出。之前是这样的:

Junit5/Jupiter – 在 Maven 构建期间打印方法名称

使用插件后,变成了这样:

Junit5/Jupiter – 在 Maven 构建期间打印方法名称

英文:

In the comment section khmarbaise dropped a link which worked out for me quite well. I requested him to promote it as an answer instead of a comment so I could mark it as resolved, however he didn't so I am posting his answer here.

I used https://github.com/fabriciorby/maven-surefire-junit5-tree-reporter plugin to generate more detailed output. It was previously like this:

Junit5/Jupiter – 在 Maven 构建期间打印方法名称

And after using the plugin it became like this:

Junit5/Jupiter – 在 Maven 构建期间打印方法名称

答案2

得分: -1

你可以使用 org.junit.jupiter.api.extension.TestWatcher 扩展来实现这一点。

  1. 创建一个类似下面的扩展类。

    package com.seenukarthi.testutils.extensions;
    
    import org.junit.jupiter.api.extension.ExtensionContext;
    import org.junit.jupiter.api.extension.TestWatcher;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.Optional;
    
    public class TestsLogExtension implements TestWatcher {
    
        private static final Logger log = LoggerFactory.getLogger("TestLogger");
    
        @Override
        public void testDisabled(ExtensionContext context, Optional<String> reason) {
            log.info("{}#{} 已禁用,因为 {}", context.getTestClass().orElse(Object.class), context.getDisplayName(), reason.orElse(""));
        }
    
        @Override
        public void testSuccessful(ExtensionContext context) {
            log.info("{}#{} 成功", context.getTestClass().orElse(Object.class), context.getDisplayName());
        }
    
        @Override
        public void testAborted(ExtensionContext context, Throwable cause) {
            log.error("{}#{} 已中止", context.getTestClass().orElse(Object.class), context.getDisplayName(), cause);
        }
    
        @Override
        public void testFailed(ExtensionContext context, Throwable cause) {
            log.error("{}#{} 失败", context.getTestClass().orElse(Object.class), context.getDisplayName());
        }
    }
    
  2. 在测试类中使用这个扩展,就像下面的例子一样。

    package com.seenukarthi.test;
    
    import com.seenukarthi.testutils.extensions.TestsLogExtension;
    import org.junit.jupiter.api.*;
    import org.junit.jupiter.api.extension.ExtendWith;
    
    @ExtendWith({TestsLogExtension.class})
    class BeanTest {
        @Test
        void init() {
            // 你的测试用例。
        }
    }
    

这将打印测试方法及其结果。

英文:

You can achieve this using org.junit.jupiter.api.extension.TestWatcher extension.

  1. Create an extension class like below.
    package com.seenukarthi.testutils.extensions;
    
    import org.junit.jupiter.api.extension.ExtensionContext;
    import org.junit.jupiter.api.extension.TestWatcher;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.Optional;
    
    public class TestsLogExtension implements TestWatcher {
    
        private static final Logger log = LoggerFactory.getLogger(&quot;TestLogger&quot;);
    
        @Override
        public void testDisabled(ExtensionContext context, Optional&lt;String&gt; reason) {
            log.info(&quot;{}#{} is disabled because {}&quot;,
                context.getTestClass().orElse(Object.class),
                context.getDisplayName(), reason.orElse(&quot;&quot;));
        }
    
        @Override
        public void testSuccessful(ExtensionContext context) {
            log.info(&quot;{}#{} is Success&quot;,
                context.getTestClass().orElse(Object.class), context.getDisplayName());
        }
    
        @Override
        public void testAborted(ExtensionContext context, Throwable cause) {
            log.error(&quot;{}#{} is aborted&quot;,
                context.getTestClass().orElse(Object.class), context.getDisplayName(), cause);
        }
    
        @Override
        public void testFailed(ExtensionContext context, Throwable cause) {
            log.error(&quot;{}#{} is failed&quot;,
                context.getTestClass().orElse(Object.class), context.getDisplayName());
        }
    }
    
    
  2. Use the extension in your test classes like the one below.
    package com.seenukarthi.test;
    
    import com.seenukarthi.testutils.extensions.TestsLogExtension;
    import org.junit.jupiter.api.*;
    import org.junit.jupiter.api.extension.ExtendWith;
    
    @ExtendWith({TestsLogExtension.class})
    class BeanTest {
        @Test
        void init() {
            //Your test case.
        }
    }
    

This will print the method tested with the results.

huangapple
  • 本文由 发表于 2023年4月17日 19:20:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定