JUnit测试说明 [英] JUnit test description

查看:86
本文介绍了JUnit测试说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JUnit中是否可以为将来的读者添加测试的简短描述(例如,正在测试的内容,一些简短的解释,预期的结果……)?我的意思是类似ScalaTest的东西,我可以在其中写:

Is it possible in JUnit to add a brief description of the test for the future reader (e.g. what's being tested, some short explanation, expected result, ...)? I mean something like in ScalaTest, where I can write:

test("Testing if true holds") {
  assert(true)
}

理想的方法是使用一些注释,例如

Ideal approach would be using some annotation, e.g.

@Test
@TestDescription("Testing if true holds")
public void testTrue() {
    assert(true);
}

因此,如果我使用Maven(或某些类似工具)运行此类带注释的测试,则使用ScalaTest时,我的输出可能与SBT中的输出类似:

Therefore, if I run such annotated tests using Maven (or some similar tool), I could have similar output to the one I have in SBT when using ScalaTest:

- Testing if entity gets saved correctly
- Testing if saving fails when field Name is not specified
- ...

当前,我可以使用非常长的方法名称,也可以编写javadoc注释,这些注释是 在构建输出中不存在.

Currently I can either use terribly long method names or write javadoc comments, which are not present in the build output.

谢谢.

推荐答案

在JUnit 5中,有

In JUnit 5, there is @DisplayName annotation:

@DisplayName用于声明自定义显示名称. 带注释的测试类或测试方法.通常使用显示名称 用于在IDE和构建工具中进行测试报告,其中可能包含空格, 特殊字符,甚至表情符号.

@DisplayName is used to declare a custom display name for the annotated test class or test method. Display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.

示例:

@Test
@DisplayName("Test if true holds")
public void checkTrue() {
    assertEquals(true, true);
}

这篇关于JUnit测试说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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