https://github.com/varabyte/truthish
A Kotlin multiplatform unit testing library inspired by / similar to Google Truth.
https://github.com/varabyte/truthish
hacktoberfest kotlin kotlin-android kotlin-ios kotlin-js kotlin-jvm kotlin-library kotlin-linux kotlin-macos kotlin-mingw kotlin-multiplatform unit-testing
Last synced: about 1 month ago
JSON representation
A Kotlin multiplatform unit testing library inspired by / similar to Google Truth.
- Host: GitHub
- URL: https://github.com/varabyte/truthish
- Owner: varabyte
- License: apache-2.0
- Created: 2019-02-18T06:24:45.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-02-07T22:40:04.000Z (9 months ago)
- Last Synced: 2025-04-02T04:34:38.261Z (7 months ago)
- Topics: hacktoberfest, kotlin, kotlin-android, kotlin-ios, kotlin-js, kotlin-jvm, kotlin-library, kotlin-linux, kotlin-macos, kotlin-mingw, kotlin-multiplatform, unit-testing
- Language: Kotlin
- Homepage:
- Size: 499 KB
- Stars: 117
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-kotlin-multiplatform - Truthish - Multiplatform library with a testing API inspired by Google Truth. (Libraries / Test)
README
## Truthish





[](https://bsky.app/profile/bitspittle.bsky.social)
A testing API inspired by [Google Truth](https://github.com/google/truth) but
rewritten in Kotlin from the ground up, so it can be used in Kotlin
multiplatform projects.
For example, you can write `assertThat` checks in tests like this:
```kotlin
import com.varabyte.truthish.*
fun isEven(num: Int) = (num % 2) == 0
fun square(num: Int) = (num * num)
@Test
fun testEvenOdd() {
assertThat(isEven(1234)).isTrue()
assertThat(isEven(1235)).isFalse()
}
@Test
fun testSum() {
val nums = listOf(1, 2, 3, 4, 5)
assertThat(nums.sum()).isEqualTo(15)
}
@Test
fun testMap() {
assertThat(listOf(1, 2, 3, 4, 5).map { square(it) })
.containsExactly(1, 4, 9, 16, 25)
.inOrder()
}
@Test
fun customMessage() {
assertWithMessage("Unexpected list size")
.that(listOf(1, 2, 3, 4, 5)).hasSize(5)
}
@Test
fun testDivideByZeroException() {
val ex = assertThrows {
10 / 0
}
assertThat(ex.message).isEqualTo("/ by zero")
}
```
If you would like to check multiple assertions at the same time (meaning a failure won't be reported until all checks
have run), you can use the `assertAll` function:
```kotlin
val person = Person("Alice", 30)
assertAll {
that(person.name).isEqualTo("Bob")
that(person.age).isEqualTo(45)
}
// The above will assert once, reporting both equality check failures
```
You can read the [Google Truth documentation](https://truth.dev/) for why they
believe their fluent approach to assertions is both more readable and produces
cleaner error messages, but let's break one of the tests above to see a
specific example error message:
```kotlin
@Test
fun testMapButIntentionallyBroken() {
assertThat(listOf(1, 2, 3, 4, 5).map { square(it) })
.containsExactly(1, 4, 9, 15, 26) // <-- Ooops, messed up 16 and 25 here
.inOrder()
}
```
Output:
```text
A collection did not contain element(s)
Expected exactly all elements from: [ 1, 4, 9, 15, 26 ]
But was : [ 1, 4, 9, 16, 25 ]
Missing : [ 15, 26 ]
Extraneous : [ 16, 25 ]
```
# Using Truthish in Your Project
## Multiplatform
To use *Truthish* in your multiplatform application, declare any of the following dependencies relevant to your project:
```kotlin
// build.gradle.kts
// Multiplatform
repositories {
mavenCentral()
}
kotlin {
jvm()
js {
browser()
nodeJs()
}
wasmJs {
browser()
nodeJs()
d8()
}
linuxArm64()
linuxX64()
macosArm64() // Mac M1+
macosX64() // Mac Intel
mingwX64() // Windows
iosArm64() // iOS M1+
iosX64() // iOS Intel
iosSimulatorArm64()
androidTarget()
sourceSets {
commonTest.dependencies {
implementation("com.varabyte.truthish:truthish:1.0.3")
implementation(kotlin("test"))
}
}
}
```
## Single platform
You can also use *Truthish* in non-multiplatform projects as well:
### JVM
```kotlin
// build.gradle.kts
repositories {
mavenCentral()
}
dependencies {
// ...
testImplementation(kotlin("test"))
testImplementation("com.varabyte.truthish:truthish:1.0.3")
}
```
### Android
```kotlin
// build.gradle.kts
repositories {
mavenCentral()
}
android { /* ... */ }
dependencies {
// ...
// If used in tests that are run on the host (i.e. your dev machine)
testImplementation("com.varabyte.truthish:truthish:1.0.3")
// If used in tests that are run on the device
androidTestImplementation("com.varabyte.truthish:truthish:1.0.3")
}
```
### Testing snapshots
Most users won't ever need to run a Truthish snapshot, so feel free to skip this section! However, occasionally, bug
fixes and new features will be available for testing for a short period before they are released.
If you ever file a bug with Truthish and are asked to test a fix using a snapshot, you must add an entry for the sonatype
snapshots repository to your `repositories` block in order to allow Gradle to find it:
```diff
// build.gradle.kts
repositories {
mavenCentral()
+ maven("https://central.sonatype.com/repository/maven-snapshots/") {
+ mavenContent {
+ includeGroup("com.varabyte.truthish")
+ snapshotsOnly()
+ }
+ }
}
```