JUnit 报告——测试方法描述

发布于 2024-07-17 01:25:36 字数 452 浏览 6 评论 0原文

我想看看是否有办法通过 javadoc 在我的 junit 报告中包含“描述性文本”。 JUnit 4 似乎不像 TestNG 那样支持 @Test 注释的“描述”属性。

到目前为止,根据我的研究,只有一种名为 javadoc-junit 的工具(http://javadoc-junit .sourceforge.net/)。 但是我无法让它工作,因为它似乎与 Junit 4 不兼容。

我想要的是某种方式在 JUnit 报告中为我的每个测试方法提供一两句话文本。 JavaDoc 不好,因为目标受众必须在 JavaDoc 和 Junit Report 之间切换才能查看文档和/或测试统计数据。

有人知道我可以毫不费力地使用其他任何东西吗?

最好的, 雷杰

I am trying to see if there is a way to include "descriptive text" in my junit reports by way of javadocs. JUnit 4 doesnt seem to support the 'description' attribute for the @Test annotation like TestNG does.

So far from what I have researched there is only one tool out there called javadoc-junit (http://javadoc-junit.sourceforge.net/). However I could not get this to work since it seems to be incompatible with Junit 4.

What I want is some way to provide a sentence or two of text with my each test method in the JUnit report. JavaDoc is no good since the target audience will have to swtich between JavaDoc and the Junit Report to see documentation and/or test stats.

Anyone know of anything else I could use with minimal effort?

Best,
Ray J

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独患者 2024-07-24 01:25:36

JUnit 5 中,有一种方法可以使用 @DisplayName 注释每个测试。 声明的测试类可以包含文本特殊字符表情符号

每个测试的声明文本对测试运行者测试报告可见。


Javadoc 说:

public @interface DisplayName

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

以及用户指南

import org.junit.gen5.api.DisplayName;
import org.junit.gen5.api.Test;

@DisplayName("A special test case")
class DisplayNameDemo {

@Test
@DisplayName("Custom test name containing spaces")
void testWithDisplayNameContainingSpaces() {
}

@Test
@DisplayName("╯°□°)╯")
void testWithDisplayNameContainingSpecialCharacters() {
}

@Test
@DisplayName("

In JUnit 5 there is a way to annotate every test with a @DisplayName. The declared test classes can have text, special characters and emojis.

The declared text on each test is visible by test runners and test reports.


The Javadoc says:

public @interface DisplayName

@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.

And the User Guide:

import org.junit.gen5.api.DisplayName;
import org.junit.gen5.api.Test;

@DisplayName("A special test case")
class DisplayNameDemo {

    @Test
    @DisplayName("Custom test name containing spaces")
    void testWithDisplayNameContainingSpaces() {
    }

    @Test
    @DisplayName("╯°□°)╯")
    void testWithDisplayNameContainingSpecialCharacters() {
    }

    @Test
    @DisplayName("????")
    void testWithDisplayNameContainingEmoji() {
    }
}
入怼 2024-07-24 01:25:36

还有一个相当新的解决方案,称为Allure。 那是一个基于Java的测试执行报告,主要是在代码中添加补充注释。 现有注释包括:

  • 自定义描述:@Description("A Cool test")
  • 按功能或故事分组:@Features({"feature1", "feature2"})@Stories({"story1", "story2" })
  • 将测试用例内执行的方法标记为步骤:@Step(甚至适用于私有方法)
  • 附件: @Attachment(name = "页面截图", type = "image/png")

查看他们的wiki示例项目 了解更多细节。

There's also rather recent solution called Allure. That's a Java-based test execution report mainly based on adding supplementary annotations to the code. Existing annotations include:

  • custom description: @Description("A cool test")
  • grouping by features or stories: @Features({"feature1", "feature2"}), @Stories({"story1", "story2" })
  • marking methods executed inside test case as steps: @Step (works even for private methods)
  • attachments: @Attachment(name = "Page screenshot", type = "image/png")

See their wiki and example project for more details.

jJeQQOZ5 2024-07-24 01:25:36

我不会将 javadoc 放入 JUnit 测试中。 我通常使方法的名称具有足够的描述性,因此它与我能想到的任何评论一样好,甚至更好。

I don't put javadocs in JUnit tests. I usually make the name of the method descriptive enough so it's as good as or better than any comment I could come up with.

著墨染雨君画夕 2024-07-24 01:25:36

我可以想象,集成测试框架 (FIT) 将是一个很好且干净的解决方案。

FIT 做什么?
FIT 是一个框架,允许通过 Word 文档中的表格、wiki 表格或 html 表格编写测试。
FIT 会忽略表格之外的每个字符,并让您输入文档、描述、要求等。

这些表看起来怎么样?

想象一个函数MyMath.square(int),它计算输入参数的平方。 您必须构建一个所谓的 Fixture,作为 MyMath 和下表之间的适配器:

class.with.Fixture.Square
x    square()
2    4
5    25

第一列描述输入值,第二列描述预期结果。 如果不相等,则该字段标记为红色。

灯具是什么样的?
对于给定的示例,这将是正确的装置:

package class.with.Fixture // Must be the same as in the fist row of the table

public class Square extends Fixture {
    public int x; // Must be the same as in the second row
    public int square() { // Must be the same as in the second row
        return MyMath.square(x);
    }
}

也许您可以使用 FIT 来满足您的要求。
请随意评论我的答案或编辑您的问题以获取更多信息!

I could imagine, that the Framework for Integrated Tests (FIT) would be a nice and clean solution.

What does FIT do?
FIT is a framework that allows to write tests via a table in a Word document, a wiki table or an html table.
Every character outside of a table is ignored by FIT and let you enter documentation, description, requirements and so on.

How does on of these tables look like?

Imagine a function MyMath.square(int) that squares it's input parameter. You have to build a so called Fixture, being an adapter between your MyMath and the following table:

class.with.Fixture.Square
x    square()
2    4
5    25

The first column describes input values, the second the expected result. If it's not equal, this field is marked as red.

How does a Fixture look like?
For the given example, this would be the correct fixture:

package class.with.Fixture // Must be the same as in the fist row of the table

public class Square extends Fixture {
    public int x; // Must be the same as in the second row
    public int square() { // Must be the same as in the second row
        return MyMath.square(x);
    }
}

Probably, you can use FIT for your requirements.
Feel free to comment my answer or edit your question for more information!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文