BDD assertion library for Kotlin
Latest commit 055b072 Sep 21, 2016 @winterbe committed on GitHub Merge pull request #4 from hastebrot/feat-expect-sequence
Add basic support for Sequence<T>
Failed to load latest commit information.
src Add expect() and should for Sequence<T>. Add ExpectSequenceTest. Sep 21, 2016
.gitignore initial commit Feb 7, 2016
.travis.yml Travis config Feb 19, 2016 More docs Feb 26, 2016
LICENSE initial commit Feb 7, 2016 Version 0.5.0 Sep 20, 2016
pom.xml Version 0.5.0 Sep 20, 2016

Expekt Travic CI

Expekt is a (work in progress) BDD assertion library for Kotlin, inspired by Chai.js. It works with your favorite test runner such as JUnit and Spek.

class ExpektTest {
    fun helloExpekt() {
        listOf(1, 2, 3).should.have.size.above(1)

Follow on Twitter for updates!

Getting started

Expekt is available via Maven Central. Just add the dependency to your Maven POM or Gradle build config.

testCompile "com.winterbe:expekt:0.5.0"


Expekt let's you write assertions in natural english language by building fluent sentences in your JUnit tests.

It comes in two flavors should and expect, both exposing the same API. It's up to you which variant to use. The property should is available on any object (e.g. myObject.should), even on null. The function expect accepts any object as parameter (e.g. expect(myObject)) instead.

When using IntelliJ IDEA you can simply use expect and should from classpath. The IDE handles all imports for you. In case you have to handle imports manually, add one of those to your test file:

import com.winterbe.expekt.expect
import com.winterbe.expekt.should

The Expekt API consists of many chainable properties and functions. Properties like to, be and which are provided to improve readibility. They don't serve any semantical meaning. The property not is used to negate expectations. Depending on the type of the initial value plenty of properties and functions are available to assert different aspects of the value, e.g. you can assert that a collection contains some elements, that a number is within it's bounds or that a string matches a given regex pattern.

See API doc for all available assertion properties and functions.

What happens when expectations fail?

When an expectation fails Expekt throws a java.lang.AssertionError containing a readable message, so you can easily see what's going wrong.

class FailingTest {
    fun thisTestFails() {, delta = 0.1)

The above test fails, resulting in the following exception:

java.lang.AssertionError: 3.4 should be closeTo 3.2 ±0.1

    at com.winterbe.expekt.ExpectAny.verify(ExpectAny.kt:68)
    at com.winterbe.expekt.ExpectDouble.closeTo(ExpectDouble.kt:12)
    at com.example.ExampleTest.example1(ExampleTest.kt:10)


Example assertions using the should property:

3.should.satisfy { it % 2 == 1 }
listOf(1, 2, 3).should.contain(3).and.have.length.above(2)
listOf(1, 2, 3).should.contain.any.elements(1, 3, 4)
listOf(1, 2, 3).should.have.all.elements(1, 2, 3)
mapOf("foo" to "bar", "bar" to "foo").should.contain("foo" to "bar")

Example assertions using the expect function:

expect(3) { it % 2 == 1 }
expect(listOf(1, 2, 3)).to.contain(3)
expect(listOf(1, 2, 3)).to.contain.any.elements(1, 3, 4)
expect(listOf(1, 2, 3)).to.have.all.elements(1, 2, 3)
expect(mapOf("foo" to "bar", "bar" to "foo")).to.contain("foo" to "bar")