Skip to content

Commit

Permalink
Polish parameterized test support in user guide
Browse files Browse the repository at this point in the history
Issue: #14
  • Loading branch information
sbrannen authored and marcphilipp committed Apr 1, 2017
1 parent b591a04 commit cdf3132
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions documentation/src/docs/asciidoc/writing-tests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ to the console.
=== Parameterized Tests

Parameterized tests make it possible to run a test multiple times with different arguments. They are
declared just like regular `@Test` methods, but use the `@ParameterizedTest` annotation instead. In
declared just like regular `@Test` methods but use the `@ParameterizedTest` annotation instead. In
addition, you must declare at least one _source_ that will provide the arguments for each
invocation.

Expand All @@ -551,15 +551,16 @@ testWithStringParameter(String) ✔
[[writing-tests-parameterized-tests-setup]]
==== Required Setup

In order to use parameterized tests you need to add a dependency to the `junit-jupiter-params`
In order to use parameterized tests you need to add a dependency on the `junit-jupiter-params`
artifact. Please refer to <<dependency-metadata>> for details.

[[writing-tests-parameterized-tests-sources]]
==== Sources of Arguments

Out of the box, JUnit Jupiter already provides quite a few _source_ annotations. The following
subsections provide a brief overview and an example for each of them. Please refer to the Javadoc in
the `{params-provider-package}` package for additional information.
Out of the box, JUnit Jupiter provides quite a few _source_ annotations. Each of the
following subsections provides a brief overview and an example for each of them. Please
refer to the JavaDoc in the `{params-provider-package}` package for additional
information.

[[writing-tests-parameterized-tests-sources-ValueSource]]
===== @ValueSource
Expand Down Expand Up @@ -592,7 +593,7 @@ include::{testDir}/example/ParameterizedTestDemo.java[tags=EnumSource_example]

`@MethodSource` allows to refer to one or multiple methods of the test class. Each method must
return a `Stream`, an `Iterable`, an `Iterator`, or an array of arguments. In addition, each method
must be `static` and don't use any parameters.
must be `static` and must not accept any arguments.

If you only need a single parameter, you can return instances of the parameter type directly as
demonstrated by the following example.
Expand All @@ -614,7 +615,8 @@ include::{testDir}/example/ParameterizedTestDemo.java[tags=multi_arg_MethodSourc
[[writing-tests-parameterized-tests-sources-CsvSource]]
===== @CsvSource

`@CsvSource` allows to express argument lists as comma-separated `String` literals.
`@CsvSource` allows you to express argument lists as comma-separated values (i.e.,
`String` literals).

[source,java,indent=0]
[subs="verbatim"]
Expand All @@ -625,8 +627,8 @@ include::{testDir}/example/ParameterizedTestDemo.java[tags=CsvSource_example]
[[writing-tests-parameterized-tests-sources-CsvFileSource]]
===== @CsvFileSource

`@CsvFileSource` lets you use CSV files from the classpath. Each line leads to one invocation of the
parameterized test.
`@CsvFileSource` lets you use CSV files from the classpath. Each line from a CSV file
results in one invocation of the parameterized test.

[source,java,indent=0]
[subs="verbatim"]
Expand Down Expand Up @@ -660,11 +662,11 @@ include::{testDir}/example/ParameterizedTestDemo.java[tags=ArgumentsSource_examp
===== Implicit Conversion

To support use cases like `@CsvSource`, JUnit Jupiter provides a number of built-in implicit
conversions. The conversion process depends on the declared type of each method parameter.
type converters. The conversion process depends on the declared type of each method parameter.

For example, if a `@ParameterizedTest` declares a parameter of type `TimeUnit` and the actual type
supplied by the declared source is a `String`, it will automatically be converted into the
corresponding enum constant.
For example, if a `@ParameterizedTest` declares a parameter of type `TimeUnit` and the
actual type supplied by the declared source is a `String`, the string will automatically
be converted into the corresponding `TimeUnit` enum constant.

[source,java,indent=0]
[subs="verbatim"]
Expand Down Expand Up @@ -725,17 +727,18 @@ include::{testDir}/example/ParameterizedTestDemo.java[tags=explicit_java_time_co
[[writing-tests-parameterized-tests-display-names]]
==== Customizing Display Names

By default, the display name of a parameterized test invocation containers the invocation index and
the `String` representation of all arguments. You can customize the invocation display names by
using the `name` parameter of the `@ParameterizedTest` annotation like in the following example.
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.
However, you can customize invocation display names via the `name` attribute of the
`@ParameterizedTest` annotation like in the following example.

[source,java,indent=0]
[subs="verbatim"]
----
include::{testDir}/example/ParameterizedTestDemo.java[tags=custom_display_names]
----

When executing this method using the `ConsoleLauncher` you will see output similar to the following.
When executing the above method using the `ConsoleLauncher` you will see output similar to the following.

[source]
[subs="verbatim"]
Expand All @@ -746,7 +749,7 @@ Display name of container ✔
└─ 3 ==> first='baz, qux', second=3 ✔
----

The following placeholders are available:
The following placeholders are supported within custom display names.

[cols="20,80"]
|===
Expand All @@ -761,10 +764,11 @@ The following placeholders are available:
[[writing-tests-parameterized-tests-lifecycle-interop]]
==== Lifecycle and Interoperability

Each invocation of a parameterized test has the same lifecycle as regular `@Test` methods. For
example, `@BeforeEach` methods will be executed before each invocation. Similar to
<<writing-tests-dynamic-tests>>, invocations will appear one by one in the test tree of an IDE. You
may at will mix regular `@Test` methods and `@ParameterizedTest` methods within the same test class.
Each invocation of a parameterized test has the same lifecycle as a regular `@Test`
method. For example, `@BeforeEach` methods will be executed before each invocation.
Similar to <<writing-tests-dynamic-tests>>, invocations will appear one by one in the
test tree of an IDE. You may at will mix regular `@Test` methods and `@ParameterizedTest`
methods within the same test class.

You may use `ParameterResolver` extensions with `@ParameterizedTest` methods. However, method
parameters that are resolved by argument sources need to come first in the argument list.
Expand Down

0 comments on commit cdf3132

Please sign in to comment.