• Stars
    star
    172
  • Rank 221,201 (Top 5 %)
  • Language
    Kotlin
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

BDD assertion library for Kotlin

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 {
    @Test
    fun helloExpekt() {
        23.should.equal(23)
        "Kotlin".should.not.contain("Scala")
        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.

Maven
<dependency>
    <groupId>com.winterbe</groupId>
    <artifactId>expekt</artifactId>
    <version>0.5.0</version>
    <scope>test</scope>
</dependency>
Gradle
testCompile "com.winterbe:expekt:0.5.0"

Introduction

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 {
    @Test
    fun thisTestFails() {
        3.4.should.be.closeTo(3.2, 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.fail(ExpectAny.kt:77)
	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)

Examples

Example assertions using the should property:

23.should.equal(23)
null.should.be.`null`
"foo".should.not.equal("bar")
3.should.satisfy { it % 2 == 1 }
3.should.be.above(2).and.below(4)
"abc".should.contain("bc").and.startWith("a")
"abc".should.not.have.length.above(3)
"abc".should.not.match(Regex("[0-9]+"))
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(23).to.equal(23)
expect(null).to.be.`null`
expect("foo").not.to.equal("bar")
expect(3).not.to.satisfy { it % 2 == 1 }
expect(3).to.be.above(2).and.to.be.below(4)
expect("abc").to.contain("bc").and.to.startWith("a")
expect("abc").not.to.have.length.above(3)
expect("abc").not.to.match(Regex("[0-9]+"))
expect(listOf(1, 2, 3)).to.contain(3).and.to.have.length.above(2)
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")

License

MIT

More Repositories

1

java8-tutorial

Modern Java - A Guide to Java 8
Java
16,488
star
2

streamjs

Lazy Object Streaming Pipeline for JavaScript
JavaScript
863
star
3

github-matrix-screensaver

The GitHub Matrix Screensaver for Mac OSX
JavaScript
554
star
4

spring-react-example

Isomorphic Spring Boot React.js Example
JavaScript
526
star
5

sequency

⚑️ Type-safe functional sequences for processing iterable data
TypeScript
338
star
6

mobx-logger

Log Mobx Actions, Reactions, Transactions and Computations
JavaScript
229
star
7

github-matrix

The GitHub Matrix
Java
206
star
8

spring-kotlin-react-demo

Demo Webapp using SpringBoot, Kotlin and React.js
JavaScript
186
star
9

react-samples

Just a bunch of React.js examples
JavaScript
77
star
10

nake

A simplified Make for Java 8 Nashorn
JavaScript
72
star
11

jest-teamcity-reporter

Teamcity Reporter for Jest Unittest Results
JavaScript
50
star
12

java8-explorer

Java 8 API Explorer
Java
49
star
13

RNTimerExample

πŸ“± React Native + Mobx sample app
JavaScript
42
star
14

javadoc-reloaded

Proposal for JEP 225: Javadoc Search
CSS
35
star
15

kotlin-examples

Kotlin code samples
Kotlin
15
star
16

algorithms

Various Algorithms written in ES6
JavaScript
11
star
17

challenge

Programming challenges
Kotlin
10
star
18

chaya

An HTML5 Web Chat
JavaScript
9
star
19

dotfiles

My personal dotfiles for Mac OSX terminal
Shell
7
star
20

chrome-bookmarks

Extension for Google Chrome to show your bookmarks in a compact popup
JavaScript
4
star
21

android-moneysheet

Money Sheet for Android
Java
3
star
22

compary

A library for composing complex compare-functions in JavaScript
TypeScript
2
star
23

sway

TypeScript
1
star
24

FoodTracker

Sample iOS App written in Swift
Swift
1
star
25

langs

Learning new programming languages
Swift
1
star