猿问

Junit5 TestReporter

我试图了解 Junit5 中的 TestReporter


@BeforeEach

void beforeEach(TestInfo testInfo) {


}


@ParameterizedTest

@ValueSource(strings = "foo")

void testWithRegularParameterResolver(String argument, TestReporter testReporter) {

    testReporter.publishEntry("argument", argument);

}


@AfterEach

void afterEach(TestInfo testInfo) {

    // ...

}

有什么用的publishEntry在TestReporter,


有人可以解释我.. 提前致谢..


绝地无双
浏览 160回答 3
3回答

qq_花开花谢_0

“TestReporter”与“TestInfo”一起给出了当前测试的一个实例,这样您就可以获得有关实际测试的信息。然后发布它,在这个例子中用作一种记录器。StringBuffer 用于测试所需的可变、快速和同步特性。public class TestReporterTest {&nbsp; &nbsp; StringBuffer sbtags = new StringBuffer();&nbsp; &nbsp; StringBuffer displayName = new StringBuffer();&nbsp; &nbsp; StringBuffer className = new StringBuffer();&nbsp; &nbsp; StringBuffer methodName = new StringBuffer();&nbsp; &nbsp; @BeforeEach&nbsp; &nbsp; void init(TestInfo testInfo) {&nbsp; &nbsp; &nbsp; &nbsp; className.delete( 0, className.length());&nbsp; &nbsp; &nbsp; &nbsp; className.append( testInfo.getTestClass().get().getName());&nbsp; &nbsp; &nbsp; &nbsp; displayName.delete( 0, displayName.length());&nbsp; &nbsp; &nbsp; &nbsp; displayName.append( testInfo.getDisplayName());&nbsp; &nbsp; &nbsp; &nbsp; methodName.delete( 0, methodName.length());&nbsp; &nbsp; &nbsp; &nbsp; methodName.append( testInfo.getTestMethod().get().getName());&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; @DisplayName("testing on reportSingleValue")&nbsp; &nbsp; void reportSingleValue(TestReporter testReporter) {&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry( "className&nbsp; : " + className);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry( "displayName: " + displayName);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry("methodName&nbsp; : " + methodName);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry("algun mensaje de estatus");&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; void reportKeyValuePair(TestReporter testReporter) {&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry( "className&nbsp; : " + className);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry( "displayName: " + displayName);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry("methodName&nbsp; : " + methodName);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry("una Key", "un Value");&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; void reportMultiKeyValuePairs(TestReporter testReporter) {&nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> map = new HashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; map.put("Fast and Furious 8","2018");&nbsp; &nbsp; &nbsp; &nbsp; map.put("Matrix","1999");&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry( "className&nbsp; : " + className);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry( "displayName: " + displayName);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry("methodName&nbsp; : " + methodName);&nbsp; &nbsp; &nbsp; &nbsp; testReporter.publishEntry(map);&nbsp; &nbsp; }}运行测试timestamp = 2019-11-22T12:02:45.898, value = className&nbsp; : TestReporterTesttimestamp = 2019-11-22T12:02:45.904, value = displayName: testing on reportSingleValuetimestamp = 2019-11-22T12:02:45.904, value = methodName&nbsp; : reportSingleValuetimestamp = 2019-11-22T12:02:45.904, value = algun mensaje de estatustimestamp = 2019-11-22T12:02:45.919, value = className&nbsp; : TestReporterTesttimestamp = 2019-11-22T12:02:45.920, value = displayName: reportMultiKeyValuePairs(TestReporter)timestamp = 2019-11-22T12:02:45.920, value = methodName&nbsp; : reportMultiKeyValuePairstimestamp = 2019-11-22T12:02:45.921, Fast and Furious 8 = 2018, Matrix = 1999timestamp = 2019-11-22T12:02:45.924, value = className&nbsp; : TestReporterTesttimestamp = 2019-11-22T12:02:45.925, value = displayName: reportKeyValuePair(TestReporter)timestamp = 2019-11-22T12:02:45.925, value = methodName&nbsp; : reportKeyValuePairtimestamp = 2019-11-22T12:02:45.925, una Key = un Value

繁花如伊

除了前面的答案,当我们在编写 junit 测试脚本时,如果我们想从流程中获取一些信息,我们通常会执行 System.out.println,这在公司/企业世界中是不推荐的。特别是在代码审查、同行审查中,我们建议从代码库中删除所有 System.out.println。因此,在junit 世界中,如果我们想要推送或发布脚本,我们建议使用TestReporter publishEntry() 方法。通过结合TestInfo,我们可以从原始junit 脚本中读取一些信息。希望这个事实也支持你的问题。
随时随地看视频慕课网APP

相关分类

Java
我要回答