Skip to content

Commit

Permalink
Add assume function to abort tests instead of failing them
Browse files Browse the repository at this point in the history
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
evant committed Dec 5, 2023
1 parent c9209ca commit e73f725
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

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

### Fixed
- Fixed incorrect usage of contains in some kdoc examples
Expand Down
4 changes: 4 additions & 0 deletions assertk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ kotlin {
}
}
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
15 changes: 15 additions & 0 deletions assertk/src/jvmMain/kotlin/assertk/assume.kt
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() }
}
21 changes: 21 additions & 0 deletions assertk/src/jvmMain/kotlin/assertk/failure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

package assertk

import com.willowtreeapps.opentest4k.AssertionFailedError
import com.willowtreeapps.opentest4k.TestAbortedException

internal actual inline fun failWithNotInStacktrace(error: Throwable): Nothing {
val filtered = error.stackTrace
.dropWhile { it.className.startsWith("assertk") }
Expand All @@ -16,3 +19,21 @@ internal actual inline fun failWithNotInStacktrace(error: Throwable): Nothing {
internal actual inline fun Throwable.isFatal(): Boolean =
// https://github.com/ReactiveX/RxJava/blob/6a44e5d0543a48f1c378dc833a155f3f71333bc2/src/main/java/io/reactivex/exceptions/Exceptions.java#L66
this is VirtualMachineError || this is ThreadDeath || this is LinkageError

internal object AssumptionFailure : Failure {
override fun fail(error: Throwable) {
failWithNotInStacktrace(
TestAbortedException(
buildString {
append("Assumption failed")
error.message?.let { message ->
append(": ")
append(message)
}
},
// unwrap assertion errors
if (error is AssertionFailedError) error.cause else error
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import assertk.all
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.*
import org.junit.Test
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
Expand Down
28 changes: 28 additions & 0 deletions assertk/src/jvmTest/kotlin/test/assertk/assertions/AssumeTest.kt
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() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package test.assertk.assertions

import assertk.assertThat
import assertk.assertions.isSuccess
import org.junit.Test
import test.assertk.exceptionPackageName
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import assertk.all
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.*
import org.junit.Test
import java.time.LocalDate
import java.time.Month
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import assertk.assertThat
import assertk.assertions.isNegative
import assertk.assertions.isPositive
import assertk.assertions.isZero
import org.junit.Test
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

Expand Down

0 comments on commit e73f725

Please sign in to comment.