Skip to content

Commit e73f725

Browse files
committed
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
1 parent c9209ca commit e73f725

File tree

9 files changed

+80
-4
lines changed

9 files changed

+80
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2323

2424
### Added
2525
- Added `doesNotContainKey` assertion for `Map`
26+
- Added `assume` to support test assumptions. To use, wrap your assertions
27+
```kotlin
28+
assume {
29+
assertThat(System.getProperty("os.name")).startsWith("Windows")
30+
}
31+
```
32+
this will cause the test to be skipped instead of failing.
33+
Note: This feature only works with opentest4j-compatible testing frameworks like junit5.
2634

2735
### Fixed
2836
- Fixed incorrect usage of contains in some kdoc examples

assertk/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ kotlin {
6464
}
6565
}
6666
}
67+
68+
tasks.withType<Test>().configureEach {
69+
useJUnitPlatform()
70+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package assertk
2+
3+
/**
4+
* Aborts the test instead of failing it, this gives you a way to skip tests based on a dynamic assertion.
5+
*
6+
* ```
7+
* // only run test on windows
8+
* assume {
9+
* assertThat(System.getProperty("os.name")).startsWith("Windows")
10+
* }
11+
* ```
12+
*/
13+
fun assume(f: () -> Unit) {
14+
AssumptionFailure.run { f() }
15+
}

assertk/src/jvmMain/kotlin/assertk/failure.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
package assertk
55

6+
import com.willowtreeapps.opentest4k.AssertionFailedError
7+
import com.willowtreeapps.opentest4k.TestAbortedException
8+
69
internal actual inline fun failWithNotInStacktrace(error: Throwable): Nothing {
710
val filtered = error.stackTrace
811
.dropWhile { it.className.startsWith("assertk") }
@@ -16,3 +19,21 @@ internal actual inline fun failWithNotInStacktrace(error: Throwable): Nothing {
1619
internal actual inline fun Throwable.isFatal(): Boolean =
1720
// https://github.com/ReactiveX/RxJava/blob/6a44e5d0543a48f1c378dc833a155f3f71333bc2/src/main/java/io/reactivex/exceptions/Exceptions.java#L66
1821
this is VirtualMachineError || this is ThreadDeath || this is LinkageError
22+
23+
internal object AssumptionFailure : Failure {
24+
override fun fail(error: Throwable) {
25+
failWithNotInStacktrace(
26+
TestAbortedException(
27+
buildString {
28+
append("Assumption failed")
29+
error.message?.let { message ->
30+
append(": ")
31+
append(message)
32+
}
33+
},
34+
// unwrap assertion errors
35+
if (error is AssertionFailedError) error.cause else error
36+
)
37+
)
38+
}
39+
}

assertk/src/jvmTest/kotlin/test/assertk/JVMAssertAllTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import assertk.all
44
import assertk.assertAll
55
import assertk.assertThat
66
import assertk.assertions.*
7-
import org.junit.Test
87
import java.util.concurrent.Executors
98
import java.util.concurrent.TimeUnit
9+
import kotlin.test.Test
1010
import kotlin.test.assertEquals
1111
import kotlin.test.assertFailsWith
1212
import kotlin.test.assertFalse
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.assertk.assertions
2+
3+
import assertk.assertThat
4+
import assertk.assertions.isFalse
5+
import assertk.assume
6+
import com.willowtreeapps.opentest4k.TestAbortedException
7+
import kotlin.test.Test
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertFailsWith
10+
11+
class AssumeTest {
12+
@Test
13+
fun assume_throws_TestAbortedException() {
14+
val error = assertFailsWith<TestAbortedException> {
15+
assume {
16+
assertThat(true).isFalse()
17+
}
18+
}
19+
20+
assertEquals("Assumption failed: expected to be false", error.message)
21+
}
22+
23+
@Test
24+
fun assume_aborts_instead_of_fails_test() {
25+
// this test should be skipped instead of failing
26+
assume { assertThat(true).isFalse() }
27+
}
28+
}

assertk/src/jvmTest/kotlin/test/assertk/assertions/JVMResultTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package test.assertk.assertions
22

33
import assertk.assertThat
44
import assertk.assertions.isSuccess
5-
import org.junit.Test
65
import test.assertk.exceptionPackageName
6+
import kotlin.test.Test
77
import kotlin.test.assertEquals
88
import kotlin.test.assertFailsWith
99
import kotlin.test.assertNotNull

assertk/src/jvmTest/kotlin/test/assertk/assertions/OptionalTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import assertk.all
44
import assertk.assertAll
55
import assertk.assertThat
66
import assertk.assertions.*
7-
import org.junit.Test
87
import java.time.LocalDate
98
import java.time.Month
109
import java.util.*
10+
import kotlin.test.Test
1111
import kotlin.test.assertEquals
1212
import kotlin.test.assertFailsWith
1313

assertk/src/jvmTest/kotlin/test/assertk/assertions/support/JavaNumberTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import assertk.assertThat
44
import assertk.assertions.isNegative
55
import assertk.assertions.isPositive
66
import assertk.assertions.isZero
7-
import org.junit.Test
87
import java.math.BigDecimal
98
import java.math.BigInteger
9+
import kotlin.test.Test
1010
import kotlin.test.assertEquals
1111
import kotlin.test.assertFailsWith
1212

0 commit comments

Comments
 (0)