ps-aux
ps-aux

Reputation: 12146

Human readable test names in JUnit 4

How can I specify human readable names of tests (to be shown in IDE and test reports) with JUnit 4?

Ideally by automatically converting actual test name.

Upvotes: 8

Views: 12223

Answers (1)

Stefan Birkner
Stefan Birkner

Reputation: 24520

I suppose that "human readable names" primarily means test names with spaces.

Underscores

My favorite solution for this problem is using underscores instead of spaces. This is the only solution that works together with IDEs nicely. E.g. you can jump from the test execution panel to the test with a single mouse click.

@Test
public void the_server_accepts_connections_with_password() {
    ...
}

Have a look at FakeSftpServerRuleTest for a complete test class that uses this style.

Test Runners

There are other solutions, but all of them require you to write an own Runner.

You can build a Runner that supports something similar to JUnit Jupiter's @DisplayName annotation.

You can build a Runner that converts the underscores from the test method's name to spaces for the display name of the test.

The problem with these solutions is that the IDE can no longer associate test results with the test method.

Non-standard Spaces

Warning: Don't do it!

Just for completeness. There are various space characters in Unicode and some of them can be part of method names in Java. You can write test names using them. You still have proper IDE support. Nevertheless: don't do it. The reader of the code will not realise that magic and become desperate.

Upvotes: 7

Related Questions