Skip to content

Commit

Permalink
Make TestNG ScenarioTestListener work with parallel="tests" option (#409
Browse files Browse the repository at this point in the history
)
  • Loading branch information
janschaefer committed Sep 2, 2019
1 parent 9b31b55 commit 4481185
Showing 1 changed file with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.tngtech.jgiven.impl.ScenarioHolder;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.tngtech.jgiven.base.ScenarioTestBase;
import com.tngtech.jgiven.impl.ScenarioBase;
import com.tngtech.jgiven.impl.ScenarioHolder;
import com.tngtech.jgiven.impl.util.AssertionUtil;
import com.tngtech.jgiven.impl.util.ParameterNameUtil;
import com.tngtech.jgiven.report.impl.CommonReportHelper;
Expand All @@ -26,8 +26,7 @@
public class ScenarioTestListener implements ITestListener {

public static final String SCENARIO_ATTRIBUTE = "jgiven::scenario";

private volatile ConcurrentMap<String, ReportModel> reportModels;
public static final String REPORT_MODELS_ATTRIBUTE = "jgiven::reportModels";

@Override
public void onTestStart( ITestResult paramITestResult ) {
Expand All @@ -43,9 +42,9 @@ public void onTestStart( ITestResult paramITestResult ) {
}

ScenarioHolder.get().setScenarioOfCurrentThread( scenario );
paramITestResult.setAttribute(SCENARIO_ATTRIBUTE, scenario);
paramITestResult.setAttribute( SCENARIO_ATTRIBUTE, scenario );

ReportModel reportModel = getReportModel( instance.getClass() );
ReportModel reportModel = getReportModel( paramITestResult, instance.getClass() );
scenario.setModel( reportModel );
scenario.getExecutor().injectStages( instance );

Expand All @@ -56,11 +55,13 @@ public void onTestStart( ITestResult paramITestResult ) {
scenario.getExecutor().readScenarioState( instance );
}

private ScenarioBase getScenario(ITestResult paramITestResult ) {
return (ScenarioBase) paramITestResult.getAttribute(SCENARIO_ATTRIBUTE);
private ScenarioBase getScenario( ITestResult paramITestResult ) {
return (ScenarioBase) paramITestResult.getAttribute( SCENARIO_ATTRIBUTE );
}

private ReportModel getReportModel( Class<?> clazz ) {
private ReportModel getReportModel(ITestResult testResult, Class<?> clazz) {
ConcurrentHashMap<String, ReportModel> reportModels = getReportModels(testResult.getTestContext());

ReportModel model = reportModels.get( clazz.getName() );
if( model == null ) {
model = new ReportModel();
Expand Down Expand Up @@ -108,16 +109,22 @@ public void onTestFailedButWithinSuccessPercentage( ITestResult paramITestResult

@Override
public void onStart( ITestContext paramITestContext ) {
reportModels = new ConcurrentHashMap<String, ReportModel>();
paramITestContext.setAttribute(REPORT_MODELS_ATTRIBUTE, new ConcurrentHashMap<String, ReportModel>());
}

@Override
public void onFinish( ITestContext paramITestContext ) {
ConcurrentHashMap<String, ReportModel> reportModels = getReportModels(paramITestContext);
for( ReportModel reportModel : reportModels.values() ) {
new CommonReportHelper().finishReport( reportModel );
}
}

private ConcurrentHashMap<String, ReportModel> getReportModels(ITestContext paramITestContext) {
return (ConcurrentHashMap<String, ReportModel>)
paramITestContext.getAttribute(REPORT_MODELS_ATTRIBUTE);
}

private List<NamedArgument> getArgumentsFrom( Method method, ITestResult paramITestResult ) {
return ParameterNameUtil.mapArgumentsWithParameterNames( method, asList( paramITestResult.getParameters() ) );
}
Expand Down

0 comments on commit 4481185

Please sign in to comment.