A Kotlin multiplatform unit testing library inspired by / similar to Google Truth.

Overview

Truthish

version Varabyte Discord

A testing API inspired by 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:

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<ArithmeticException> {
        10 / 0
    }
    assertThat(ex.message).isEqualTo("/ by zero")
}

You can read the Google Truth documentation 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:

@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:

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 the following dependencies:

// build.gradle
// Multiplatform

repositories {
  mavenCentral()
}

kotlin {
    jvm()
    js(IR) {
      browser()
    }
    // ...
    sourceSets {
        // ...
        commonTest {
            dependencies {
                implementation kotlin("test-common")
                implementation kotlin("test-annotations-common")
                // ...
                implementation "com.varabyte.truthish:truthish:0.6.3"
            }
        }

        jmvTest {
            dependencies {
                implementation kotlin("test")
                implementation "com.varabyte.truthish:truthish-jvm:0.6.3"
            }
        }

        jsTest {
            dependencies {
                implementation kotlin("test-js")
                implementation "com.varabyte.truthish:truthish-js:0.6.3"
            }
        }
    }
}

Single platform

You can also use Truthish in non-multiplatform projects as well:

// build.gradle
// JVM

repositories {
  /* ... */
  maven { url 'https://us-central1-maven.pkg.dev/varabyte-repos/public' }
}

dependencies {
  // ...

  testImplementation "org.jetbrains.kotlin:kotlin-test"
  testImplementation "com.varabyte.truthish:truthish:0.6.3"
}
You might also like...
Successor to ProxyBuilder - Uses Spring & Netty for testing proxies, interacting with a local MariaDB.

Successor to ProxyBuilder - Uses Spring & Netty for testing proxies, interacting with a local MariaDB.

Example code from coroutines testing talk.

Coroutines Testing This repo contains example code used to build my (Bill Phillips) talk on coroutines testing. Use it however you please, but note so

Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

🔓 Kotlin version of the popular google/easypermissions wrapper library to simplify basic system permissions logic on Android M or higher.
🔓 Kotlin version of the popular google/easypermissions wrapper library to simplify basic system permissions logic on Android M or higher.

EasyPermissions-ktx Kotlin version of the popular googlesample/easypermissions wrapper library to simplify basic system permissions logic on Android M

A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android

Blue-Falcon A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS, Android, MacOS, Raspberry Pi and Javascript. Bluetooth in general has t

Dependency Injection library for Kotlin Multiplatform, support iOS and Android

Multiplatform-DI library for Kotlin Multiplatform Lightweight dependency injection framework for Kotlin Multiplatform application Dependency injection

Kotlin multiplatform library template.

template-kmp-library Kotlin multiplatform library template. Has a baseline setup for a multiplatform library supporting all kotlin targets except andr

Generic AST parsing library for kotlin multiplatform

kotlinx.ast kotlinx.ast is a generic AST (Abstract Syntax Tree) parsing library, Kotlin is currently the only supported language. The library is desig

Server Sent Events (SSE) client multiplatform library made with Kotlin and backed by coroutines

OkSSE OkSSE is an client for Server Sent events protocol written in Kotlin Multiplatform. The implementation is written according to W3C Recommendatio

Comments
  • Unexpected inOrder success that should be failing

    Unexpected inOrder success that should be failing

    It seems like having a duplicate value in a list can confuse the inOrder asserter

        @Test
        fun uhOh() {
            assertThat(listOf("3", "2", "1", "30", "20", "10", "20")).containsExactly("3", "2", "1", "30", "20", "20", "10").inOrder()
        }
    
    opened by bitspittle 1
  • Sequence test is sometimes not getting consumed

    Sequence test is sometimes not getting consumed

    Sequences are funky -- running foreach on it twice ends up consuming the sequence.

    Everywhere we work with iterables, we probably need to convert them into lists first, e.g. containsExactly

    opened by bitspittle 0
  • Feature Request: Support assertThrows with a message

    Feature Request: Support assertThrows with a message

    Currently, all the assertThat methods have assertWithMessage versions.

    assertThrows is kind of special and I don't think we can do quite the same thing, but there should be some way to add support for a custom message.

    I had a loop in some of my code that tested several inputs against an assertThrows check, but when one of them failed, I didn't have any idea of which one. It would have been nice to do something like

    for (badInput in listOf(...)) {
       assertThrows<...>("Testing exceptions for input case $badInput...") { ... }
    }
    
    opened by bitspittle 0
Releases(v0.6.3)
  • v0.6.3(May 18, 2022)

    A very minor release (a single bug fix, mainly), but thought I'd create it in preparation for releasing on maven central.

    • Fixed an issue with the in order asserter (contains(...).inOrder()) screwing up if there were multiple elements in the original list that equalled each other's values.
    • Updated Kotlin to 1.6.20
    • Tweaked build.gradle to enable me to publish on Maven Central
    Source code(tar.gz)
    Source code(zip)
  • v0.6.2(Apr 4, 2022)

  • v0.6.1(Apr 1, 2022)

  • v0.6.0(Sep 16, 2021)

    The initial release of trurthish.

    At this point, you should be able to test about any project, but the tires haven't really been kicked by normal users much yet, so I hesitate to call it stable. It's possible some useful subjects or methods are missing, or that the output from some of them can use some massaging.

    Once I've had a chance to use this in a few projects or maybe get feedback from the community, I'd be more confident bumping this up to a 1.0 release.

    Source code(tar.gz)
    Source code(zip)
Owner
Varabyte
Varabyte
WordMasterKMP - WIP Kotlin Multiplatform sample inspired by Wordle and also Word Master web sample

WordMasterKMP WIP Kotlin Multiplatform sample inspired by Wordle and also Word M

John O'Reilly 56 Oct 4, 2022
Sample application to demonstrate Multi-module Clean MVVM Architecture and usage of Android Hilt, Kotlin Flow, Navigation Graph, Unit tests etc.

MoneyHeist-Chars Sample application to demonstrate Multi-module Clean MVVM Architecture and usage of Android Hilt, Kotlin Flow, Navigation Graph, Room

Hisham 20 Nov 19, 2022
Architecture With MVI using Kotlin, Coroutines, Retrofit and Unit test

Architecture With MVI using Kotlin, Coroutines, Retrofit and Unit test MVI (Model-View-Intent) streamlines the process of creating and developing appl

Ahmed Karam 4 Aug 18, 2022
Minimal UI library for Android inspired by React

Anvil - reactive views for Android Anvil is a small Java library for creating reactive user interfaces. Originally inspired by React, it suits well as

null 1.4k Dec 23, 2022
Netflix inspired OTT Home Screen, Contains implementation in Reactjs, Kotlin React Wrapper, Jetpack Compose Web

Netflix-Clone-React Practising React by building Netflix Clone Requirements TMDB api key : Add TMDB API key to AppApi.kt Learning Resourcce Build Netf

Chetan Gupta 61 Nov 13, 2022
Kotlin DSL inspired by bhailang.js

Kotlin DSL inspired by bhailang.js

kaiwalya 1 Mar 22, 2022
Simple random ore mod for Minecraft/Fabric. Heavily inspired by Randomite.

Rand'Ore >> Download << Simple random ores for Fabric! This mod is open source and under a permissive license. As such, it can be included in any modp

null 3 Dec 31, 2021
Simple random ore mod for Minecraft/Rift. Heavily inspired by Randomite.

Rand'Ore >> Download << Simple random ores for Rift! This mod is open source and under a permissive license. As such, it can be included in any modpac

Peakstep 1 Jul 10, 2022
Free and Open Source monster taming video game. Inspired by traditional RPGs like Pokémon, Final Fantasy and Golden Sun.

Welcome Help Wanted: I cannot finish this on my own. If you are willing to help, let me know. GuardianMonsters is a Free and Open Source monster tamin

Georg Eckert 2 Oct 17, 2022
Utility for developers and QAs what helps minimize time wasting on writing the same data for testing over and over again. Made by Stfalcon

Stfalcon Fixturer A Utility for developers and QAs which helps minimize time wasting on writing the same data for testing over and over again. You can

Stfalcon LLC 31 Nov 29, 2021