-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue-1465: Make JGiven pass on assertions from JUnit5
and also test that passing on assumption failures works for all testframeworks Signed-off-by: l-1squared <30831153+l-1squared@users.noreply.github.com>
- Loading branch information
1 parent
2817a83
commit b0c5815
Showing
7 changed files
with
174 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 49 additions & 19 deletions
68
jgiven-junit/src/test/java/com/tngtech/jgiven/junit/AssumptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,62 @@ | ||
package com.tngtech.jgiven.junit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.AssumptionViolatedException; | ||
import org.junit.Test; | ||
|
||
import com.tngtech.jgiven.annotation.Description; | ||
import com.tngtech.jgiven.junit.test.GivenTestStep; | ||
import com.tngtech.jgiven.junit.test.ThenTestStep; | ||
import com.tngtech.jgiven.junit.test.WhenTestStep; | ||
import com.tngtech.jgiven.report.model.ScenarioCaseModel; | ||
import com.tngtech.jgiven.report.model.StepStatus; | ||
import org.junit.Assume; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.Assumptions.assumeThat; | ||
|
||
@Description("Scenarios can have sections") | ||
public class AssumptionTest extends SimpleScenarioTest<AssumptionTest.TestStage> { | ||
|
||
@Description( "Scenarios can have sections" ) | ||
public class AssumptionTest extends ScenarioTest<GivenTestStep, WhenTestStep, ThenTestStep> { | ||
|
||
@Test | ||
public void JUnit_assumption_exceptions_should_be_treated_correctly() throws Throwable { | ||
try { | ||
when().some_assumption_fails(); | ||
Assertions.fail( "AssumptionViolationException should have been thrown" ); | ||
} catch( AssumptionViolatedException e ) {} | ||
public void should_pass_on_assertJ_assumptions() throws Throwable { | ||
assertThatThrownBy(() -> when().I_assume_something_using_assertJ()) | ||
.isInstanceOf(catchException(AssumptionTest::assertJAssumptionFailure)); | ||
getScenario().finished(); | ||
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0); | ||
assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.PASSED); | ||
} | ||
|
||
@Test | ||
public void should_pass_on_junit5_assumptions() throws Throwable { | ||
assertThatThrownBy(() -> when().I_assume_something_using_junit5()) | ||
.isInstanceOf(catchException(AssumptionTest::junitAssumptionFailure)); | ||
getScenario().finished(); | ||
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase(0); | ||
assertThat(aCase.getStep(0).getStatus()).isEqualTo(StepStatus.PASSED); | ||
} | ||
|
||
static class TestStage { | ||
void I_assume_something_using_assertJ() { | ||
assertJAssumptionFailure(); | ||
} | ||
|
||
void I_assume_something_using_junit5() { | ||
junitAssumptionFailure(); | ||
} | ||
} | ||
|
||
private static void assertJAssumptionFailure() { | ||
assumeThat(true).isFalse(); | ||
} | ||
|
||
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); | ||
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); | ||
@SuppressWarnings("DataFlowIssue")//we want to provoke an assumption failure | ||
private static void junitAssumptionFailure() { | ||
Assume.assumeTrue(false); | ||
} | ||
|
||
} | ||
private Class<? extends Exception> catchException(Runnable runnable) { | ||
try { | ||
runnable.run(); | ||
return null; | ||
} catch (Exception e) { | ||
return e.getClass(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
jgiven-junit5/src/test/java/com/tngtech/jgiven/junit5/test/AssumptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.tngtech.jgiven.junit5.test; | ||
|
||
import com.tngtech.jgiven.junit5.SimpleScenarioTest; | ||
import com.tngtech.jgiven.report.model.ScenarioCaseModel; | ||
import com.tngtech.jgiven.report.model.StepStatus; | ||
import org.junit.jupiter.api.Assumptions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.Assumptions.assumeThat; | ||
|
||
class AssumptionsTest extends SimpleScenarioTest<AssumptionsTest.TestStage> { | ||
|
||
|
||
@Test | ||
void should_pass_on_assertJ_assumptions() throws Throwable { | ||
assertThatThrownBy(() -> when().I_assume_something_using_assertJ()) | ||
.isInstanceOf(catchException(AssumptionsTest::assertJAssumptionFailure)); | ||
getScenario().finished(); | ||
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); | ||
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); | ||
} | ||
|
||
@Test | ||
void should_pass_on_junit5_assumptions() throws Throwable { | ||
assertThatThrownBy(() -> when().I_assume_something_using_junit5()) | ||
.isInstanceOf(catchException(AssumptionsTest::junitAssumptionFailure)); | ||
getScenario().finished(); | ||
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); | ||
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); | ||
} | ||
|
||
static class TestStage { | ||
void I_assume_something_using_assertJ() { | ||
assertJAssumptionFailure(); | ||
} | ||
|
||
void I_assume_something_using_junit5() { | ||
junitAssumptionFailure(); | ||
} | ||
} | ||
|
||
private static void assertJAssumptionFailure(){ | ||
assumeThat( true ).isFalse(); | ||
} | ||
|
||
private static void junitAssumptionFailure(){ | ||
Assumptions.abort(); | ||
} | ||
private Class<? extends Exception> catchException(Runnable runnable) { | ||
try { | ||
runnable.run(); | ||
return null; | ||
} catch (Exception e) { | ||
return e.getClass(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ public void JGiven_works_with_JUnit5_DISABLED() { | |
then().some_outcome(); | ||
} | ||
|
||
|
||
} |
59 changes: 59 additions & 0 deletions
59
jgiven-testng/src/test/java/com/tngtech/jgiven/testng/AssumptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.tngtech.jgiven.testng; | ||
|
||
import com.tngtech.jgiven.report.model.ScenarioCaseModel; | ||
import com.tngtech.jgiven.report.model.StepStatus; | ||
import org.testng.SkipException; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.Assumptions.assumeThat; | ||
|
||
@Test(singleThreaded = true) | ||
public class AssumptionsTest extends SimpleScenarioTest<AssumptionsTest.TestStage> { | ||
|
||
|
||
@Test | ||
public void should_pass_on_assertJ_assumptions() throws Throwable { | ||
assertThatThrownBy(() -> when().I_assume_something_using_assertJ()) | ||
.isInstanceOf(catchException(AssumptionsTest::assertJAssumptionFailure)); | ||
getScenario().finished(); | ||
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); | ||
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); | ||
} | ||
|
||
@Test | ||
public void should_pass_on_junit5_assumptions() throws Throwable { | ||
assertThatThrownBy(() -> when().I_assume_something_using_junit5()) | ||
.isInstanceOf(catchException(AssumptionsTest::testNgAssumptionFailure)); | ||
getScenario().finished(); | ||
ScenarioCaseModel aCase = getScenario().getModel().getLastScenarioModel().getCase( 0 ); | ||
assertThat( aCase.getStep( 0 ).getStatus() ).isEqualTo( StepStatus.PASSED ); | ||
} | ||
|
||
static class TestStage { | ||
void I_assume_something_using_assertJ() { | ||
assertJAssumptionFailure(); | ||
} | ||
|
||
void I_assume_something_using_junit5() { | ||
testNgAssumptionFailure(); | ||
} | ||
} | ||
|
||
private static void assertJAssumptionFailure(){ | ||
assumeThat( true ).isFalse(); | ||
} | ||
|
||
private static void testNgAssumptionFailure(){ | ||
throw new SkipException("TestNG assumption failure"); | ||
} | ||
private Class<? extends Exception> catchException(Runnable runnable) { | ||
try { | ||
runnable.run(); | ||
return null; | ||
} catch (Exception e) { | ||
return e.getClass(); | ||
} | ||
} | ||
} |