'Set the test name for DynamicTest
I'm leveraging the DynamicTest
functionality of JUnit 5.3.0-M1 and there's a showstopping point I can't seem to figure out with regards to the test name.
My expectation (right or wrong) is that the displayName
of the DynamicTest
translates to what I'm used to seeing as the test name in the test output.
For example, given the following TestFactory
method
public class RequestAcceptTest {
private final Proxy.Location proxy = Proxy.location();
@TestFactory
Collection<DynamicTest> testAllMimeTypes() {
final List<DynamicTest> tests = new ArrayList<>();
for (final Method method : Method.values()) {
for (final MimeType mimeType : MimeType.values()) {
final String displayName = String.format("testAccept%svia%s", mimeType.name(), method);
tests.add(dynamicTest(displayName, () -> {
assertAccept(200, method, mimeType);
}));
}
}
return tests;
}
I would expect to see failures output by maven-surefire-plugin contain the DynamicTest
displayName somewhere, possibly like so:
[ERROR] Tests run: 6, Failures: 6, Errors: 0, Skipped: 0, Time elapsed: 1,578.898 s <<< FAILURE! - in com.example.prototype.proxy.RequestAcceptTest
[ERROR] testAcceptApplicationJsonviaGET Time elapsed: 0.013 s <<< FAILURE!
org.opentest4j.AssertionFailedError
at com.example.prototype.proxy.RequestAcceptTest.assertAccept(RequestAcceptTest.java:73)
at com.example.prototype.proxy.RequestAcceptTest.lambda$testAllMimeTypes$0(RequestAcceptTest.java:64)
[ERROR] testAcceptApplicationJsonviaPUT Time elapsed: 0.001 s <<< FAILURE!
org.opentest4j.AssertionFailedError
at com.example.prototype.proxy.RequestAcceptTest.assertAccept(RequestAcceptTest.java:73)
at com.example.prototype.proxy.RequestAcceptTest.lambda$testAllMimeTypes$0(RequestAcceptTest.java:64)
[ERROR] testAcceptApplicationJsonviaPOST Time elapsed: 0.001 s <<< FAILURE!
org.opentest4j.AssertionFailedError
at com.example.prototype.proxy.RequestAcceptTest.assertAccept(RequestAcceptTest.java:73)
at com.example.prototype.proxy.RequestAcceptTest.lambda$testAllMimeTypes$0(RequestAcceptTest.java:64)
Instead it is array indexed.
[ERROR] Tests run: 6, Failures: 6, Errors: 0, Skipped: 0, Time elapsed: 1,578.898 s <<< FAILURE! - in com.example.prototype.proxy.RequestAcceptTest
[ERROR] testAllMimeTypes[1] Time elapsed: 0.013 s <<< FAILURE!
org.opentest4j.AssertionFailedError
at com.example.prototype.proxy.RequestAcceptTest.assertAccept(RequestAcceptTest.java:73)
at com.example.prototype.proxy.RequestAcceptTest.lambda$testAllMimeTypes$0(RequestAcceptTest.java:64)
[ERROR] testAllMimeTypes[2] Time elapsed: 0.001 s <<< FAILURE!
org.opentest4j.AssertionFailedError
at com.example.prototype.proxy.RequestAcceptTest.assertAccept(RequestAcceptTest.java:73)
at com.example.prototype.proxy.RequestAcceptTest.lambda$testAllMimeTypes$0(RequestAcceptTest.java:64)
[ERROR] testAllMimeTypes[3] Time elapsed: 0.001 s <<< FAILURE!
org.opentest4j.AssertionFailedError
at com.example.prototype.proxy.RequestAcceptTest.assertAccept(RequestAcceptTest.java:73)
at com.example.prototype.proxy.RequestAcceptTest.lambda$testAllMimeTypes$0(RequestAcceptTest.java:64)
As well, the failure summary just shows the lambda names and does not contain any reference to displayName
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] RequestAcceptTest.lambda$testAllMimeTypes$0:64->assertAccept:73
[ERROR] RequestAcceptTest.lambda$testAllMimeTypes$0:64->assertAccept:73
[ERROR] RequestAcceptTest.lambda$testAllMimeTypes$0:64->assertAccept:73
In reality, I don't have 6 or 10 test, but 10,000. I don't care how, but I do need some information I control to show up in the test output.
If I'm unable to communicate to the casual observer what the test was, then the feature is unfortunately not usable and I'll probably have to go back to bytecode generating test methods.
I also do not know if this is a JUnit question or a Maven-Surefire-Plugin question.
Solution 1:[1]
Maven took back surefire integration but didnt integrate with displayname yet :(. https://github.com/junit-team/junit5/issues/990
Romain
Solution 2:[2]
If you configure maven-surefire-plugin, then the XML report will contain testcase name as "A() - B", where:
- A is method name with annotation @TestFactory
- B is is displayName from DynamicTest
pom.xml/build/plugins:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
<configuration>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
</statelessTestsetReporter>
</configuration>
</plugin>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Romain Manni-Bucau |
Solution 2 | Martin Sznapka |