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

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

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. 创建一个类似下面的扩展类。

    1. package com.seenukarthi.testutils.extensions;
    2. import org.junit.jupiter.api.extension.ExtensionContext;
    3. import org.junit.jupiter.api.extension.TestWatcher;
    4. import org.slf4j.Logger;
    5. import org.slf4j.LoggerFactory;
    6. import java.util.Optional;
    7. public class TestsLogExtension implements TestWatcher {
    8. private static final Logger log = LoggerFactory.getLogger("TestLogger");
    9. @Override
    10. public void testDisabled(ExtensionContext context, Optional<String> reason) {
    11. log.info("{}#{} 已禁用,因为 {}", context.getTestClass().orElse(Object.class), context.getDisplayName(), reason.orElse(""));
    12. }
    13. @Override
    14. public void testSuccessful(ExtensionContext context) {
    15. log.info("{}#{} 成功", context.getTestClass().orElse(Object.class), context.getDisplayName());
    16. }
    17. @Override
    18. public void testAborted(ExtensionContext context, Throwable cause) {
    19. log.error("{}#{} 已中止", context.getTestClass().orElse(Object.class), context.getDisplayName(), cause);
    20. }
    21. @Override
    22. public void testFailed(ExtensionContext context, Throwable cause) {
    23. log.error("{}#{} 失败", context.getTestClass().orElse(Object.class), context.getDisplayName());
    24. }
    25. }
  2. 在测试类中使用这个扩展,就像下面的例子一样。

    1. package com.seenukarthi.test;
    2. import com.seenukarthi.testutils.extensions.TestsLogExtension;
    3. import org.junit.jupiter.api.*;
    4. import org.junit.jupiter.api.extension.ExtendWith;
    5. @ExtendWith({TestsLogExtension.class})
    6. class BeanTest {
    7. @Test
    8. void init() {
    9. // 你的测试用例。
    10. }
    11. }

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

英文:

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

  1. Create an extension class like below.
    1. package com.seenukarthi.testutils.extensions;
    2. import org.junit.jupiter.api.extension.ExtensionContext;
    3. import org.junit.jupiter.api.extension.TestWatcher;
    4. import org.slf4j.Logger;
    5. import org.slf4j.LoggerFactory;
    6. import java.util.Optional;
    7. public class TestsLogExtension implements TestWatcher {
    8. private static final Logger log = LoggerFactory.getLogger(&quot;TestLogger&quot;);
    9. @Override
    10. public void testDisabled(ExtensionContext context, Optional&lt;String&gt; reason) {
    11. log.info(&quot;{}#{} is disabled because {}&quot;,
    12. context.getTestClass().orElse(Object.class),
    13. context.getDisplayName(), reason.orElse(&quot;&quot;));
    14. }
    15. @Override
    16. public void testSuccessful(ExtensionContext context) {
    17. log.info(&quot;{}#{} is Success&quot;,
    18. context.getTestClass().orElse(Object.class), context.getDisplayName());
    19. }
    20. @Override
    21. public void testAborted(ExtensionContext context, Throwable cause) {
    22. log.error(&quot;{}#{} is aborted&quot;,
    23. context.getTestClass().orElse(Object.class), context.getDisplayName(), cause);
    24. }
    25. @Override
    26. public void testFailed(ExtensionContext context, Throwable cause) {
    27. log.error(&quot;{}#{} is failed&quot;,
    28. context.getTestClass().orElse(Object.class), context.getDisplayName());
    29. }
    30. }
  2. Use the extension in your test classes like the one below.
    1. package com.seenukarthi.test;
    2. import com.seenukarthi.testutils.extensions.TestsLogExtension;
    3. import org.junit.jupiter.api.*;
    4. import org.junit.jupiter.api.extension.ExtendWith;
    5. @ExtendWith({TestsLogExtension.class})
    6. class BeanTest {
    7. @Test
    8. void init() {
    9. //Your test case.
    10. }
    11. }

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:

确定