英文:
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:
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:
- clone the project: https://github.com/Hakky54/sslcontext-kickstart
- run
mvn clean verify
英文:
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:
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:
-
clone the project: https://github.com/Hakky54/sslcontext-kickstart
-
run
mvn clean verify
答案1
得分: 1
在评论部分,khmarbaise提供了一个链接,对我非常有帮助。我请求他将其提升为答案,而不是评论,以便我可以将其标记为已解决,但他没有这样做,所以我在这里发布了他的答案。
我使用了https://github.com/fabriciorby/maven-surefire-junit5-tree-reporter插件来生成更详细的输出。之前是这样的:
使用插件后,变成了这样:
英文:
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:
And after using the plugin it became like this:
答案2
得分: -1
你可以使用 org.junit.jupiter.api.extension.TestWatcher
扩展来实现这一点。
-
创建一个类似下面的扩展类。
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()); } }
-
在测试类中使用这个扩展,就像下面的例子一样。
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.
- 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("TestLogger"); @Override public void testDisabled(ExtensionContext context, Optional<String> reason) { log.info("{}#{} is disabled because {}", context.getTestClass().orElse(Object.class), context.getDisplayName(), reason.orElse("")); } @Override public void testSuccessful(ExtensionContext context) { log.info("{}#{} is Success", context.getTestClass().orElse(Object.class), context.getDisplayName()); } @Override public void testAborted(ExtensionContext context, Throwable cause) { log.error("{}#{} is aborted", context.getTestClass().orElse(Object.class), context.getDisplayName(), cause); } @Override public void testFailed(ExtensionContext context, Throwable cause) { log.error("{}#{} is failed", context.getTestClass().orElse(Object.class), context.getDisplayName()); } }
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论