Skip to content

Commit

Permalink
Polish "Customizing Display Names" documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jun 2, 2024
1 parent 6c5a9a0 commit 69e7b53
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
27 changes: 23 additions & 4 deletions documentation/src/docs/asciidoc/user-guide/writing-tests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2209,14 +2209,15 @@ include::{testDir}/example/ParameterizedTestDemo.java[tags=ArgumentsAggregator_w
==== Customizing Display Names

By default, the display name of a parameterized test invocation contains the invocation
index and the `String` representation of all arguments for that specific invocation.
Each of them is preceded by the parameter name (unless the argument is only available via
an `ArgumentsAccessor` or `ArgumentAggregator`), if present in the bytecode (for Java,
test code must be compiled with the `-parameters` compiler flag).
index and the `String` representation of all arguments for that specific invocation. Each
argument is preceded by its parameter name (unless the argument is only available via an
`ArgumentsAccessor` or `ArgumentAggregator`), if the parameter name is present in the
bytecode (for Java, test code must be compiled with the `-parameters` compiler flag).

However, you can customize invocation display names via the `name` attribute of the
`@ParameterizedTest` annotation like in the following example.

======
[source,java,indent=0]
----
include::{testDir}/example/ParameterizedTestDemo.java[tags=custom_display_names]
Expand All @@ -2231,9 +2232,13 @@ Display name of container ✔
├─ 2 ==> the rank of 'banana' is 2 ✔
└─ 3 ==> the rank of 'lemon, lime' is 3 ✔
....
======

[NOTE]
====
Please note that `name` is a `MessageFormat` pattern. Thus, a single quote (`'`) needs to
be represented as a doubled single quote (`''`) in order to be displayed.
====

The following placeholders are supported within custom display names.

Expand All @@ -2257,16 +2262,30 @@ When using `@MethodSource`, `@FieldSource`, or `@ArgumentsSource`, you can provi
names for arguments using the `{Named}` API. A custom name will be used if the argument is
included in the invocation display name, like in the example below.

======
[source,java,indent=0]
----
include::{testDir}/example/ParameterizedTestDemo.java[tags=named_arguments]
----
When executing the above method using the `ConsoleLauncher` you will see output similar to
the following.
....
A parameterized test with named arguments ✔
├─ 1: An important file ✔
└─ 2: Another file ✔
....
======

[NOTE]
====
Note that `arguments(Object...)` is a static factory method defined in the
`org.junit.jupiter.params.provider.Arguments` interface.
Similarly, `named(String, Object)` is a static factory method defined in the
`org.junit.jupiter.api.Named` interface.
====

If you'd like to set a default name pattern for all parameterized tests in your project,
you can declare the `junit.jupiter.params.displayname.default` configuration parameter in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE;
import static org.junit.jupiter.params.provider.EnumSource.Mode.MATCH_ALL;
Expand Down Expand Up @@ -48,6 +49,7 @@
import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.AggregateWith;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
Expand All @@ -69,8 +71,14 @@
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;

@Execution(SAME_THREAD)
class ParameterizedTestDemo {

@BeforeEach
void printDisplayName(TestInfo testInfo) {
System.out.println(testInfo.getDisplayName());
}

// tag::first_example[]
@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
Expand Down

0 comments on commit 69e7b53

Please sign in to comment.