GithubHelp home page GithubHelp logo

presleyhank / kotlintest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kotest/kotest

0.0 1.0 0.0 3.48 MB

Powerful, elegant and flexible test framework for Kotlin

License: Apache License 2.0

Kotlin 99.61% Java 0.38% Shell 0.01%

kotlintest's Introduction

KotlinTest

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")

The withClue and asClue helpers can add extra context to assertions so failures are self explanatory:

withClue("Name should be present") { user.name shouldNotBe null }

data class HttpResponse(val status: Int, body: String)
val response = HttpResponse(200, "the content")
response.asClue {
    it.status shouldBe 200
    it.body shouldBe "the content"
}

Nesting is allowed in both cases and will show all available clues.

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 invocations, parallelism, 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 {
  testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
}

or with Gradle Kotlin DSL

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

dependencies {
    testImplementation("io.kotlintest:kotlintest-runner-junit5:3.3.2")
}

or with gradle and Android

android {
...
    testOptions {
        unitTests.all {
            useJUnitPlatform()
        }
    }
}

dependencies {
  testImplementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
}

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.22.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.3.2</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.3.2</version>
    <scope>test</scope>
</dependency>

kotlintest's People

Contributors

ajalt avatar andrzejressel avatar benjamin-bader avatar checketts avatar clemp6r avatar doyaaaaaken avatar dr-jenner avatar elect86 avatar faogustavo avatar hastebrot avatar helmbold avatar jeremad avatar lenalebt avatar leocolman avatar mfriedenhagen avatar michaelrocks avatar michalbrz avatar mrduguo avatar outis avatar raverona avatar reckter avatar sksamuel avatar sschuberth avatar stephenjfox avatar thibseisel avatar tibtof avatar tonilopezmr avatar vkolomeyko avatar wevnasc avatar xenomachina avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.