Skip to content

Commit

Permalink
TestNG: do not throw exceptions in the test listener, but assign them…
Browse files Browse the repository at this point in the history
… to the test result (fixes #123)
  • Loading branch information
Jan Schäfer committed Sep 25, 2015
1 parent cab8b6c commit 74af32f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.google.common.base.Throwables;
import com.tngtech.jgiven.base.ScenarioTestBase;
import com.tngtech.jgiven.impl.ScenarioBase;
import com.tngtech.jgiven.impl.util.AssertionUtil;
Expand Down Expand Up @@ -94,7 +93,7 @@ private void testFinished( ITestResult paramITestResult ) {
ScenarioBase scenario = scenarioMap.get( paramITestResult );
scenario.finished();
} catch( Throwable throwable ) {
throw Throwables.propagate( throwable );
paramITestResult.setThrowable( throwable );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ private void parametersAreHandledCorrectly( String title, int milkInLiter, Strin
List<String> arguments = scenarioCase.getExplicitArguments();
assertThat( arguments ).containsExactly( "" + milkInLiter, ingredient, "" + caseNr );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ public void ingredient( String someIngredient ) {
public void mixed_with( String something ) {}

public void something() {}

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

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.tngtech.jgiven.testng.ScenarioTest;

public class FailingCasesTestNgTest extends ScenarioTest<GivenTestStage, WhenTestStage, ThenTestStage> {

@DataProvider
public static Object[][] booleans() {
return new Object[][] {
{ true },
{ false }
};
}

@Test( dataProvider = "booleans" )
public void failing_cases_do_not_lead_to_ignored_following_cases( boolean fail ) {
given().a_failed_step( fail );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,8 @@ public static TestScenario testWithTwoCasesAndTheFirstOneFails() {
return new TestScenario( TestWithTwoCasesAndAFailingOne.class );
}

public static TestScenario testNgTestWithAFailingCase() {
return new TestScenario( FailingCasesTestNgTest.class );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ public void a_test_class_with_a_failing_scenario_and_a_failing_after_stage() {
public void a_test_with_two_cases_and_the_first_one_fails() {
testScenario = TestScenarioRepository.testWithTwoCasesAndTheFirstOneFails();
}

public void a_TestNG_test_with_two_cases_and_the_first_one_fails() {
testScenario = TestScenarioRepository.testNgTestWithAFailingCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,13 @@ public SELF the_scenario_has_derived_parameters( String... parameters ) {
return self();
}

public SELF the_report_model_contains_one_scenario_with_$_cases( int nCases ) {
assertThat( reportModel.getLastScenarioModel().getScenarioCases() ).hasSize( nCases );
return self();
}

public SELF case_$_has_status( int i, ExecutionStatus status ) {
assertThat( reportModel.getLastScenarioModel().getScenarioCases().get( i - 1 ).getExecutionStatus() ).isEqualTo( status );
return self();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.tngtech.jgiven.testng;

import org.junit.Test;

import com.tngtech.jgiven.GivenScenarioTest;
import com.tngtech.jgiven.JGivenScenarioTest;
import com.tngtech.jgiven.report.model.ExecutionStatus;
import com.tngtech.jgiven.tags.FeatureTestNg;
import com.tngtech.jgiven.tags.Issue;
import com.tngtech.jgiven.testframework.TestFramework;
import com.tngtech.jgiven.testframework.ThenTestFramework;
import com.tngtech.jgiven.testframework.WhenTestFramework;

@FeatureTestNg
public class DataProviderTestNgTest extends JGivenScenarioTest<GivenScenarioTest<?>, WhenTestFramework<?>, ThenTestFramework<?>> {

@Test
@Issue( "#123" )
public void a_scenario_with_one_failing_case_still_executes_the_following_ones() {
given().a_TestNG_test_with_two_cases_and_the_first_one_fails();
when().the_test_class_is_executed_with( TestFramework.TestNG );
then().the_report_model_contains_one_scenario_with_$_cases( 2 )
.and().the_scenario_has_execution_status( ExecutionStatus.FAILED )
.and().case_$_has_status( 1, ExecutionStatus.FAILED )
.and().case_$_has_status( 2, ExecutionStatus.SUCCESS );
}

}

0 comments on commit 74af32f

Please sign in to comment.