Changing names of parameterized tests
This feature has made it into JUnit 4.11.
To use change the name of parameterized tests, you say:
@Parameters(name="namestring")
namestring
is a string, which can have the following special placeholders:
{index}
- the index of this set of arguments. The defaultnamestring
is{index}
.{0}
- the first parameter value from this invocation of the test.{1}
- the second parameter value- and so on
The final name of the test will be the name of the test method, followed by the namestring
in brackets, as shown below.
For example (adapted from the unit test for the Parameterized
annotation):
@RunWith(Parameterized.class)
static public class FibonacciTest {
@Parameters( name = "{index}: fib({0})={1}" )
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
{ 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
}
private final int fInput;
private final int fExpected;
public FibonacciTest(int input, int expected) {
fInput= input;
fExpected= expected;
}
@Test
public void testFib() {
assertEquals(fExpected, fib(fInput));
}
private int fib(int x) {
// TODO: actually calculate Fibonacci numbers
return 0;
}
}
will give names like testFib[1: fib(1)=1]
and testFib[4: fib(4)=3]
. (The testFib
part of the name is the method name of the @Test
).
Generating display names for @ParameterizedTest in JUnit 5
As of JUnit 5.8.0, there is a Named<T>
interface as part of the JUnit Jupiter API with "automatic support for injecting the contained payload [the arguments] into parameterized methods directly" (see issue #2301). Example:
@DisplayName("A parameterized test with named arguments")
@ParameterizedTest
@MethodSource("namedArguments")
void testWithNamedArguments(File file) {}
static Stream<Arguments> namedArguments() {
return Stream.of(
Arguments.of(Named.of("An important file", new File("path1"))),
Arguments.of(Named.of("Another file", new File("path2")))
);
}
If you prefer static imports, you can also go for the corresponding aliases from Arguments
and Named
:
arguments(named("An important file", new File("path1")))
For more information, please refer to the corresponding docs.
Junit 5 : How to provide names to Parameterized Test
We can provide name using name attribute.
@ParameterizedTest(name = "withSomeName #{index} with Value [{arguments}]")
@ValueSource(strings = { "Hello", "World","Test" })
void withFixValues(String word) {
System.out.println(word);
assertNotNull(word);
}
How to specify label to a Parameterized junit run
There is an easy way to easily identify the individual test cases in a Parameterized test, you may provide a name using the @Parameters annotation.
This name is allowed to contain placeholders that are replaced at runtime:
{index}: the current parameter index
{0}, {1}, …: the first, second, and so on, parameter value
See example here:
https://github.com/junit-team/junit/wiki/Parameterized-tests
Override a pytest parameterized functions name
You're looking for the ids
argument of pytest.mark.parametrize
:
list of string ids, or a callable. If strings, each is corresponding
to the argvalues so that they are part of the test id. If callable, it
should take one argument (a single argvalue) and return a string or
return None.
Your code would look like
@pytest.mark.parametrize(
("testname", "op", "value"),
[
("testA", "plus", "3"),
("testB", "minus", "1"),
],
ids=['testA id', 'testB id']
)
def test_industry(self, testname, op, value):
Related Topics
Should I Call Ugi.Checktgtandreloginfromkeytab() Before Every Action on Hadoop
How to Import Spring-Config.Xml of One Project into Spring-Config.Xml of Another Project
Dynamically Change Spring Data Source
How to Get Absolute Path to File in /Resources Folder of Your Project
How to Get the Desktop Path in Java
In Java, When Does an Object Become Unreachable
Bigdecimal Equals() Versus Compareto()
How to Format a String in an Email So Outlook Will Print the Line Breaks
Import Xxx Cannot Be Resolved for Java Se Standard Classes
Springboot 2.6.0/Spring Fox 3 - Failed to Start Bean 'Documentationpluginsbootstrapper'
How to Use Printwriter and File Classes in Java
Entry Point for Java Applications: Main(), Init(), or Run()
Issues with Swingworker and Jprogressbar
Calculate the Display Width of a String in Java
"Loading Class Com.Mysql.Jdbc.Driver ... Is Deprecated" Message