From e8f65b99aaaa7c04e1ad3770309cd626d3fe5ab0 Mon Sep 17 00:00:00 2001 From: olivier lamy Date: Fri, 5 Jun 2020 19:22:28 +1000 Subject: [PATCH] [SUREFIRE-1797] Surefire report with parameterized tests contain all names of test the same --- .../its/JUnitPlatformRerunFailingTestsIT.java | 20 +++++ .../pom.xml | 40 +++++++++- .../java/junitplatform/ParametersTest.java | 76 +++++++++++++++++++ .../junitplatform/RunListenerAdapter.java | 20 +++-- 4 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 surefire-its/src/test/resources/junit-platform-rerun-failing-tests/src/test/java/junitplatform/ParametersTest.java diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformRerunFailingTestsIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformRerunFailingTestsIT.java index 2a3bdca406..1ae105fa21 100644 --- a/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformRerunFailingTestsIT.java +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformRerunFailingTestsIT.java @@ -203,6 +203,26 @@ public void testRerunOneTestMethod() verifyFailuresOneRetryOneMethod( outputValidator ); } + @Test + public void testParameterizedTest() + { + unpack() + .setJUnitVersion( VERSION ) + .maven() + .activateProfile( "parameters" ) + .withFailure() + .debugLogging() + .executeTest() + .assertTestSuiteResults( 6, 0, 1, 1, 0 ) + .getSurefireReportsXmlFile( "TEST-junitplatform.ParametersTest.xml" ) + .assertContainsText( "testOneFailingPassingTest(ConnectionPoolFactory)[1]" ) + .assertContainsText( "testOneFailingPassingTest(ConnectionPoolFactory)[2]" ) + .assertContainsText( "testOneFailingPassingTest(ConnectionPoolFactory)[3]" ) + .assertContainsText( "testAllPassingTest(ConnectionPoolFactory)[1]" ) + .assertContainsText( "testAllPassingTest(ConnectionPoolFactory)[2]" ) + .assertContainsText( "testAllPassingTest(ConnectionPoolFactory)[3]" ); + } + private void verifyFailuresOneRetryAllClasses( OutputValidator outputValidator ) { verifyFailuresOneRetry( outputValidator, 5, 1, 1, 0 ); diff --git a/surefire-its/src/test/resources/junit-platform-rerun-failing-tests/pom.xml b/surefire-its/src/test/resources/junit-platform-rerun-failing-tests/pom.xml index af4dde03a8..cea4be07a4 100644 --- a/surefire-its/src/test/resources/junit-platform-rerun-failing-tests/pom.xml +++ b/surefire-its/src/test/resources/junit-platform-rerun-failing-tests/pom.xml @@ -40,16 +40,54 @@ ${junit.version} test + + org.junit.jupiter + junit-jupiter-params + ${junit.version} + test + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + org.apache.maven.plugins maven-surefire-plugin - ${surefire.version} + + + **/ParametersTest.java + + + + + parameters + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/PassingTest.java + **/FlakyFirstTimeTest.java + + + + + + + diff --git a/surefire-its/src/test/resources/junit-platform-rerun-failing-tests/src/test/java/junitplatform/ParametersTest.java b/surefire-its/src/test/resources/junit-platform-rerun-failing-tests/src/test/java/junitplatform/ParametersTest.java new file mode 100644 index 0000000000..a4e4e381e4 --- /dev/null +++ b/surefire-its/src/test/resources/junit-platform-rerun-failing-tests/src/test/java/junitplatform/ParametersTest.java @@ -0,0 +1,76 @@ +package junitplatform; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; + + +public class ParametersTest +{ + public static Stream pools() + { + return Stream.of( new ConnectionPoolFactory( "duplex" ), + new ConnectionPoolFactory( "multiplex" ), + new ConnectionPoolFactory( "round-robin" ) ); + } + + @ParameterizedTest + @MethodSource( "pools" ) + public void testAllPassingTest( ConnectionPoolFactory factory ) + { + System.out.println( "testAllPassingTest factory " + factory ); + } + + @ParameterizedTest + @MethodSource( "pools" ) + public void testOneFailingPassingTest( ConnectionPoolFactory factory ) throws Exception + { + Assumptions.assumeFalse( factory.name.equals( "round-robin" ) ); + System.out.println( "Passing test factory " + factory ); + if ( factory.name.equals( "multiplex" ) ) + { + assertEquals( 1, 2 ); + } + } + + private static class ConnectionPoolFactory + { + private final String name; + + private ConnectionPoolFactory( String name ) + { + this.name = name; + } + + @Override + public String toString() + { + return name; + } + } +} diff --git a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java index 31c010a8c0..c0cfed2cbd 100644 --- a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java +++ b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/RunListenerAdapter.java @@ -22,6 +22,7 @@ import static java.util.Collections.emptyMap; import static java.util.stream.Collectors.joining; import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps; +import static org.apache.maven.surefire.shared.lang3.StringUtils.isNotBlank; import static org.junit.platform.engine.TestExecutionResult.Status.FAILED; import java.util.Map; @@ -255,13 +256,22 @@ private String[] toClassMethodName( TestIdentifier testIdentifier ) .map( s -> s.substring( 1 + s.lastIndexOf( '.' ) ) ) .collect( joining( "," ) ); - boolean hasParams = !simpleClassNames.isEmpty(); + boolean hasParams = isNotBlank( methodSource.getMethodParameterTypes() ); String methodName = methodSource.getMethodName(); - String methodSign = methodName + '(' + simpleClassNames + ')'; String description = testIdentifier.getLegacyReportingName(); - boolean useDesc = description.startsWith( methodSign ); - String methodDesc = hasParams ? ( useDesc ? description : methodSign ) : methodName; - String methodDisp = methodSign.equals( display ) ? methodDesc : display; + String methodSign = hasParams ? methodName + '(' + simpleClassNames + ')' : methodName; + boolean equalDescriptions = display.equals( description ); + boolean hasLegacyDescription = description.startsWith( methodName + '(' ); + boolean hasDisplayName = !equalDescriptions || !hasLegacyDescription; + String methodDesc = equalDescriptions || !hasParams ? methodSign : description; + String methodDisp = hasDisplayName ? display : methodDesc; + + // The behavior of methods getLegacyReportingName() and getDisplayName(). + // test || legacy | display + // ==============||==========|========== + // normal || m() | m() + // normal+displ || displ | displ + // parameterized || m()[1] | displ return new String[] {source[0], source[1], methodDesc, methodDisp}; }