Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-1711] Support @ParameterizedTest for JUnit 5 test reruns #252

Merged
merged 1 commit into from
Nov 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions surefire-providers/surefire-junit-platform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static org.apache.maven.surefire.util.TestsToRun.fromClass;
import static org.junit.platform.commons.util.StringUtils.isBlank;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request;

import java.io.IOException;
Expand Down Expand Up @@ -190,12 +190,7 @@ private LauncherDiscoveryRequest buildLauncherDiscoveryRequestForRerunFailures(
// Iterate over recorded failures
for ( TestIdentifier identifier : new HashSet<>( adapter.getFailures().keySet() ) )
{
// Extract quantified test name data
String[] classMethodName = adapter.toClassMethodNameWithoutPlan( identifier );
String className = classMethodName[0];
String methodName = classMethodName[2];
// Add filter for the specific failing method
builder.selectors( selectMethod( className, methodName ) );
builder.selectors( selectUniqueId( identifier.getUniqueId() ) );
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,6 @@ private StackTraceWriter toStackTraceWriter( String realClassName, String realMe
return new PojoStackTraceWriter( realClassName, realMethodName, throwable );
}

private String[] toClassMethodName( TestIdentifier testIdentifier )
{
return toClassMethodName( testIdentifier, true );
}

String[] toClassMethodNameWithoutPlan( TestIdentifier testIdentifier )
{
return toClassMethodName( testIdentifier, false );
}

/**
* <ul>
* <li>[0] class name - used in stacktrace parser</li>
Expand All @@ -233,11 +223,9 @@ String[] toClassMethodNameWithoutPlan( TestIdentifier testIdentifier )
* </ul>
*
* @param testIdentifier a class or method
* @param usePlan {@code true} for using the test-plan to fetch sources.
* {@code false} to rely on fallbacks.
* @return 4 elements string array
*/
private String[] toClassMethodName( TestIdentifier testIdentifier, boolean usePlan )
private String[] toClassMethodName( TestIdentifier testIdentifier )
{
Optional<TestSource> testSource = testIdentifier.getSource();
String display = testIdentifier.getDisplayName();
Expand All @@ -247,18 +235,10 @@ private String[] toClassMethodName( TestIdentifier testIdentifier, boolean usePl
MethodSource methodSource = testSource.map( MethodSource.class::cast ).get();
String realClassName = methodSource.getClassName();

String[] source;
if ( usePlan )
{
source = testPlan.getParent( testIdentifier )
.map( i -> toClassMethodName( i, usePlan ) )
.map( s -> new String[] { s[0], s[1] } )
.orElse( new String[] { realClassName, realClassName } );
}
else
{
source = new String[] { realClassName, realClassName };
}
String[] source = testPlan.getParent( testIdentifier )
.map( i -> toClassMethodName( i ) )
.map( s -> new String[] { s[0], s[1] } )
.orElse( new String[] { realClassName, realClassName } );

String methodName = methodSource.getMethodName();
boolean useMethod = display.equals( methodName ) || display.equals( methodName + "()" );
Expand All @@ -276,7 +256,7 @@ else if ( testSource.filter( ClassSource.class::isInstance ).isPresent() )
}
else
{
String source = !usePlan ? display : testPlan.getParent( testIdentifier )
String source = testPlan.getParent( testIdentifier )
.map( TestIdentifier::getDisplayName ).orElse( display );
return new String[] {source, source, display, display};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,44 @@ public void rerunWithSuccess()
assertEquals( 0, summary.getTestsFailedCount() );
}

@Test
public void rerunParameterized()
throws Exception
{
Launcher launcher = LauncherFactory.create();
ProviderParameters parameters = providerParametersMock();
// Mock the rerun variable
when( parameters.getTestRequest().getRerunFailingTestsCount() ).thenReturn( 2 );
when( parameters.getProviderProperties() )
.thenReturn( singletonMap( JUnitPlatformProvider.CONFIGURATION_PARAMETERS,
"forkCount = 1\nreuseForks = true" ) );

JUnitPlatformProvider provider = new JUnitPlatformProvider( parameters, launcher );
TestPlanSummaryListener executionListener = new TestPlanSummaryListener();
launcher.registerTestExecutionListeners( executionListener );

invokeProvider( provider, TestClass7.class );

assertThat( executionListener.summaries ).hasSize( 3 );
TestExecutionSummary summary = executionListener.summaries.get( 0 );
assertEquals( 2, summary.getTestsFoundCount() );
assertEquals( 2, summary.getTestsStartedCount() );
assertEquals( 1, summary.getTestsSucceededCount() );
assertEquals( 1, summary.getTestsFailedCount() );

summary = executionListener.summaries.get( 1 );
assertEquals( 1, summary.getTestsFoundCount() );
assertEquals( 1, summary.getTestsStartedCount() );
assertEquals( 0, summary.getTestsSucceededCount() );
assertEquals( 1, summary.getTestsFailedCount() );

summary = executionListener.summaries.get( 1 );
assertEquals( 1, summary.getTestsFoundCount() );
assertEquals( 1, summary.getTestsStartedCount() );
assertEquals( 0, summary.getTestsSucceededCount() );
assertEquals( 1, summary.getTestsFailedCount() );
}

@Test
public void allDiscoveredTestsAreInvokedForNullArgument()
throws Exception
Expand Down Expand Up @@ -921,4 +959,20 @@ void testFailTwice2()
assertTrue( count >= 3 );
}*/
}

static class TestClass7
{
static List<Object[]> params()
{
return Arrays.asList( new Object[] { "Always pass", true },
new Object[] { "Always fail", false } );
}

@org.junit.jupiter.params.ParameterizedTest
@org.junit.jupiter.params.provider.MethodSource("params")
void testParameterizedTestCases( String testName, boolean value )
{
assertTrue( value );
}
}
}