Skip to content

Commit

Permalink
[SUREFIRE-1797] Surefire report with parameterized tests contain all …
Browse files Browse the repository at this point in the history
…names of test the same
  • Loading branch information
olamy authored and Tibor17 committed Jun 9, 2020
1 parent 2e14ba6 commit e8f65b9
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,54 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<excludes>
<exclude>**/ParametersTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>parameters</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/PassingTest.java</exclude>
<exclude>**/FlakyFirstTimeTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -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<ConnectionPoolFactory> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
}
Expand Down

0 comments on commit e8f65b9

Please sign in to comment.