Skip to content

Commit

Permalink
Change exception handling for TestNG (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
janschaefer committed Sep 2, 2019
1 parent 4481185 commit 8e83ddf
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,15 @@ public void failIfPass() {
failIfPass = true;
}

public void addSection( String sectionTitle ) {
public void setCatchStepExceptions(boolean catchStepExceptions) {
methodInterceptor.setCatchExceptions(catchStepExceptions);
}

public void setSuppressExceptions(boolean suppressExceptions) {
this.suppressExceptions = suppressExceptions;
}

public void addSection(String sectionTitle ) {
listener.sectionAdded( sectionTitle );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class StepInterceptorImpl implements StepInterceptor {
*/
private boolean methodExecutionEnabled = true;

/**
* Whether all exceptions should be caught
*/
private boolean catchExceptions = true;

public StepInterceptorImpl(ScenarioExecutor scenarioExecutor, ScenarioListener listener, StageTransitionHandler stageTransitionHandler) {
this.scenarioExecutor = scenarioExecutor;
this.listener = listener;
Expand Down Expand Up @@ -207,6 +212,10 @@ public boolean enableMethodExecution( boolean b ) {
return previousMethodExecution;
}

public void setCatchExceptions(boolean b) {
catchExceptions = b;
}

private void handleMethod( Object stageInstance, Method paramMethod, Object[] arguments, InvocationMode mode,
boolean hasNestedSteps ) throws Throwable {

Expand All @@ -223,6 +232,10 @@ private void handleThrowable( Throwable t ) throws Throwable {

listener.stepMethodFailed( t );
scenarioExecutor.failed( t );

if (!catchExceptions) {
throw t;
}
}

private void handleMethodFinished( long durationInNanos, boolean hasNestedSteps ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.google.common.base.Throwables;
import org.testng.*;

import com.tngtech.jgiven.base.ScenarioTestBase;
import com.tngtech.jgiven.impl.ScenarioBase;
Expand Down Expand Up @@ -46,6 +44,14 @@ public void onTestStart( ITestResult paramITestResult ) {

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

// TestNG does not work well when catching step exceptions, so we have to disable that feature
// this mainly means that steps following a failing step are not reported in JGiven
scenario.getExecutor().setCatchStepExceptions(false);

// avoid rethrowing exceptions as they are already thrown by the steps
scenario.getExecutor().setSuppressExceptions(true);

scenario.getExecutor().injectStages( instance );

Method method = paramITestResult.getMethod().getConstructorOrMethod().getMethod();
Expand Down Expand Up @@ -90,15 +96,14 @@ public void onTestFailure( ITestResult paramITestResult ) {
}

@Override
public void onTestSkipped( ITestResult paramITestResult ) {}
public void onTestSkipped( ITestResult testResult ) {}

private void testFinished( ITestResult paramITestResult ) {
private void testFinished( ITestResult testResult ) {
try {
ScenarioBase scenario = getScenario( paramITestResult );
ScenarioBase scenario = getScenario( testResult );
scenario.finished();
} catch( Throwable throwable ) {
paramITestResult.setThrowable( throwable );
paramITestResult.setStatus( ITestResult.FAILURE );
Throwables.propagate(throwable);
} finally {
ScenarioHolder.get().removeScenarioOfCurrentThread();
}
Expand Down Expand Up @@ -128,5 +133,4 @@ private ConcurrentHashMap<String, ReportModel> getReportModels(ITestContext para
private List<NamedArgument> getArgumentsFrom( Method method, ITestResult paramITestResult ) {
return ParameterNameUtil.mapArgumentsWithParameterNames( method, asList( paramITestResult.getParameters() ) );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tngtech.jgiven.testng;

import static org.assertj.core.api.Assertions.assertThat;

import com.tngtech.jgiven.annotation.Pending;
import com.tngtech.jgiven.report.model.ExecutionStatus;
import org.assertj.core.api.Assertions;
import org.testng.SkipException;
import org.testng.annotations.Test;

import com.tngtech.jgiven.annotation.Description;
import com.tngtech.jgiven.report.model.ScenarioCaseModel;
import com.tngtech.jgiven.report.model.StepStatus;

@Description( "Pending annotation is handled correctly" )
public class PendingTest extends SimpleScenarioTest<TestNgTest.TestSteps> {

@Test
@Pending
public void pending_annotation_should_catch_exceptions() {
given().something();
when().something_fails();
then().nothing_happens();

ScenarioCaseModel aCase = getScenario().getScenarioCaseModel();
assertThat( aCase.getExecutionStatus() ).isEqualTo( ExecutionStatus.SCENARIO_PENDING );
}

@Test
public void pending_annotation_on_failing_steps_should_catch_exceptions() {
given().something();
when().something_fails_with_pending_annotation();
then().nothing_happens();

ScenarioCaseModel aCase = getScenario().getScenarioCaseModel();
assertThat( aCase.getExecutionStatus() ).isEqualTo( ExecutionStatus.SOME_STEPS_PENDING );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.tngtech.jgiven.testng;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.Test;

@Test
public class RetryTest extends SimpleScenarioTest<TestNgTest.TestSteps> {

int count = 0;

@Test(retryAnalyzer = MyAnalyzer.class)
public void failing_with_retry_test() {
when().something_should_$_fail(count++ == 0);
}

public static class MyAnalyzer implements IRetryAnalyzer {
int count = 0;
@Override
public boolean retry(ITestResult result) {
return count++ == 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import java.util.List;

import com.tngtech.jgiven.annotation.Format;
import com.tngtech.jgiven.annotation.Pending;
import com.tngtech.jgiven.format.NotFormatter;
import org.testng.annotations.Test;

import com.tngtech.jgiven.Stage;
Expand Down Expand Up @@ -86,6 +89,18 @@ public TestSteps something_fails() {
throw new IllegalStateException( "Something failed" );
}

@Pending
public TestSteps something_fails_with_pending_annotation() {
throw new IllegalStateException( "Something failed" );
}

public TestSteps something_should_$_fail(@Format(NotFormatter.class) boolean shouldFail) {
if (shouldFail) {
throw new IllegalStateException("Something failed");
}
return this;
}

public TestSteps you_get_sugar_milk() {
assertThat( result ).isEqualTo( "SugarMilk" );
return this;
Expand Down

0 comments on commit 8e83ddf

Please sign in to comment.