-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assume function to abort tests instead of failing them
jvm only, uses open4j's TestAbortedException under the hood so works on junit5 or other compatible testing framework (does not work on junit4) Fixes #432
- Loading branch information
Showing
9 changed files
with
80 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,3 +64,7 @@ kotlin { | |
} | ||
} | ||
} | ||
|
||
tasks.withType<Test>().configureEach { | ||
useJUnitPlatform() | ||
} |
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,15 @@ | ||
package assertk | ||
|
||
/** | ||
* Aborts the test instead of failing it, this gives you a way to skip tests based on a dynamic assertion. | ||
* | ||
* ``` | ||
* // only run test on windows | ||
* assume { | ||
* assertThat(System.getProperty("os.name")).startsWith("Windows") | ||
* } | ||
* ``` | ||
*/ | ||
fun assume(f: () -> Unit) { | ||
AssumptionFailure.run { f() } | ||
} |
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
28 changes: 28 additions & 0 deletions
28
assertk/src/jvmTest/kotlin/test/assertk/assertions/AssumeTest.kt
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,28 @@ | ||
package test.assertk.assertions | ||
|
||
import assertk.assertThat | ||
import assertk.assertions.isFalse | ||
import assertk.assume | ||
import com.willowtreeapps.opentest4k.TestAbortedException | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
|
||
class AssumeTest { | ||
@Test | ||
fun assume_throws_TestAbortedException() { | ||
val error = assertFailsWith<TestAbortedException> { | ||
assume { | ||
assertThat(true).isFalse() | ||
} | ||
} | ||
|
||
assertEquals("Assumption failed: expected to be false", error.message) | ||
} | ||
|
||
@Test | ||
fun assume_aborts_instead_of_fails_test() { | ||
// this test should be skipped instead of failing | ||
assume { assertThat(true).isFalse() } | ||
} | ||
} |
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
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