Pure Kotlin Unit Testing — Part 1

Write once, test everywhere

Cedric Ferry
3 min readSep 4, 2021

Introduction

JetBrains has introduced a test framework that allows developers to write pure Kotlin Unit Test.

In the past you probably used JUnit, which is a test framework base on Java (yes that’s the “J” in JUnit).

By introducing this test framework, JetBrains allows us to write Unit Tests that are working cross platform. This is obviously serving the cause of Kotlin Multiplatform, also known as “KMM”.

Importing the Kotlin test framework

First things first, we need to import the new framework into our Android or Multiplatform project.

in your build.gradle.kts, dependencies section:

implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))

Getting familiar with Kotlin test framework

While the framework is very similar to JUnit, they are some differences that worth to highlight.

The @BeforeTest and @AfterTest annotations

The @BeforeTest annotation will help you to setup the pre-requisites before each test (of your test suite).

The @AfterTest annotation will help clearing anything after each test (of your test suite)

Assert functions

--

--

Cedric Ferry
Cedric Ferry

Written by Cedric Ferry

Android Developer @ TikTok, ex-Google

No responses yet