Powerful, elegant and flexible Kotlin test framework
raverona and sksamuel Add unit tests for new matchers 'shouldNotBePositive', 'shouldNotBeNe… (
#437)

* Add unit tests for new matchers 'shouldNotBePositive', 'shouldNotBeNegative' and unit tests for both

Fixes: #435

* Fix package name for "DoubleMatchersTest.kt"

* Add documentation for the following double matchers: 'shouldBePositive', 'shouldNotBePositive', 'shouldBeNegative' and 'shouldNotBeNegative'

* Change the order of the 'shouldNotBePositive' and 'shouldNotBeNegative' tests to make it consistent through every test

* Fix unit tests implementation for matchers "shouldNotBePositive" and "shouldNotBeNegative"

the tests for cases where the implementation should fail were done all inside a "shouldThrow" block, this is not a correct approach since after the first thrown exception the subsequent tests will not execute and the test will pass without asserting all test cases

* Correct documentation for matchers "shouldNotBePositive" and "shouldNotBeNegative"

the documentation said:
- "shouldBeNotPositive" when the correct is "shouldNotBePositive"
- "shouldBeNotNegative" when the correct is "shouldNotBeNegative"
Latest commit 5671539 Oct 11, 2018
Permalink
Failed to load latest commit information.
doc Add unit tests for new matchers 'shouldNotBePositive', 'shouldNotBeNe… ( Oct 11, 2018
gradle/wrapper Add support for coroutines (#332) Jun 2, 2018
kotlintest-assertions-arrow Add arrow-assertion Failure matcher that checks underlying throwable … Oct 3, 2018
kotlintest-assertions-json Added JSON matchers module Jul 28, 2018
kotlintest-assertions-ktor Added ktor matcher for cookie values Jul 29, 2018
kotlintest-assertions Add unit tests for new matchers 'shouldNotBePositive', 'shouldNotBeNe… ( Oct 11, 2018
kotlintest-core Added tests for multiple invocations in test isolation modes Aug 9, 2018
kotlintest-extensions Removed method no longer in jdk10 Jul 29, 2018
kotlintest-intellij Disabling publish on intellij project as this is a work in progress Apr 8, 2018
kotlintest-runner Added tests for multiple invocations in test isolation modes Aug 9, 2018
kotlintest-samples Updated examples to KotlinTest 3.1.8 Jul 18, 2018
kotlintest-tests Add unit tests for new matchers 'shouldNotBePositive', 'shouldNotBeNe… ( Oct 11, 2018
.gitignore Introduced isolation mode for specs, reimplemented test runner to run… Jul 28, 2018
.travis.yml Switched travis to oracle jdk 9 not openjdk9 Jul 29, 2018
CHANGELOG.md Updated changelog for 3.1.10 Sep 13, 2018
LICENSE Update LICENSE Mar 22, 2016
README.md Fix typo in README.md (#401) Sep 10, 2018
appveyor.yml AppVeyor: Create %HOME%\.gradle independently of the configuration (#293 Apr 15, 2018
build.gradle Upgraded to kotlin 1.3 rc Oct 8, 2018
gradle.properties Set version string to 3.2.0 snapshot Jul 27, 2018
gradlew Updated travis command to use gradle wrapper Mar 19, 2018
gradlew.bat Added gradle wrapper Feb 26, 2017
settings.gradle Added JSON matchers module Jul 28, 2018

README.md

KotlinTest

Join the chat at https://gitter.im/kotlintest/lobby Build Status Build status GitHub license

KotlinTest is a flexible and comprehensive testing tool for Kotlin.
Full documentation

For latest updates see Changelog

Community

Test with Style

Write simple and beautiful tests with the StringSpec style:

class MyTests : StringSpec({
  "length should return size of string" {
    "hello".length shouldBe 5
  }
  "startsWith should test for a prefix" {
    "world" should startWith("wor")
  }
})

KotlinTest comes with several testing styles so you can choose one that fits your needs.

Multitude of Matchers

Use over 120 provided matchers to test assertions on many different types:

"substring".shouldContain("str")

user.email.shouldBeLowerCase()

myImageFile.shouldHaveExtension(".jpg")

cityMap.shouldContainKey("London")

Matchers are extension methods and so your IDE will auto complete. See the full list of matchers or write your own.

Let the Computer Generate Your Test Data

Use property based testing to test your code with automatically generated test data:

class PropertyExample: StringSpec() {
  init {
    "String size" {
      assertAll { a: String, b: String ->
        (a + b) should haveLength(a.length + b.length)
      }
    }
}

Check all the Tricky Cases With Data Driven Testing

Handle even an enormous amount of input parameter combinations easily with data driven tests:

class StringSpecExample : StringSpec({
  "maximum of two numbers" {
    forall(
        row(1, 5, 5),
        row(1, 0, 1),
        row(0, 0, 0)
    ) { a, b, max ->
      Math.max(a, b) shouldBe max
    }
  }
})

Test Exceptions

Testing for exceptions is easy with KotlinTest:

val exception = shouldThrow<IllegalAccessException> {
  // code in here that you expect to throw an IllegalAccessException
}
exception.message should startWith("Something went wrong")

Fine Tune Test Execution

You can specify the number of threads, invocations, and a timeout for each test or for all tests. And you can group tests by tags or disable them conditionally. All you need is config:

class MySpec : StringSpec() {

  override val defaultTestCaseConfig = TestCaseConfig(invocations = 3)

  init {
    "should use config".config(timeout = 2.seconds, invocations = 10, threads = 2, tags = setOf(Database, Linux)) {
      // ...
    }
  }
}

And More ...

This page gives you just a short overview of KotlinTest. There are many more features:

See full documentation.

Use

Gradle

To use in gradle, configure your build to use the JUnit Platform. For Gradle 4.6 and higher this is as simple as adding useJUnitPlatform() inside the test block and then adding the KotlinTest dependency.

test {
  useJUnitPlatform()
}

dependencies {
  testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.7'
}

or with Gradle Kotlin DSL

val test by tasks.getting(Test::class) {
    useJUnitPlatform { }
}

dependencies {
    testCompile("io.kotlintest:kotlintest-runner-junit5:3.1.9")
}

Maven

For maven you must configure the surefire plugin for junit tests.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.2.0</version>
        </dependency>
    </dependencies>
</plugin>

And then add the KotlinTest JUnit5 runner to your build.

<dependency>
    <groupId>io.kotlintest</groupId>
    <artifactId>kotlintest-runner-junit5</artifactId>
    <version>3.1.7</version>
    <scope>test</scope>
</dependency>