Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assume function to abort tests instead of failing them #500

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
* }
* ```
*/
inline 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(
evant marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package test.assertk.assertions

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isTrue
import assertk.assume
import com.willowtreeapps.opentest4k.TestAbortedException
import test.assertk.runTest
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() }
}

@Test
fun assume_does_not_capture_unexpected_exceptions() {
assertFailsWith<NullPointerException> {
assume { throw NullPointerException() }
}
}

@Test
fun assume_aborts_when_suspend() = runTest {
assume { assertThat(suspend { 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