How to write beforeEach and beforeClass in kotlintest
Asked Answered
T

3

8

Given is the example from kotlin-test github docs, but i don't see beforeEach or beforeClass concept here. I want to understand,

  • How to execute a code/method once before every test
  • How to execute a code/method once before every test class
class MyTests : StringSpec({
  "length should return size of string" {
    "hello".length shouldBe 5
  }
  "startsWith should test for a prefix" {
    "world" should startWith("wor")
  }
})
Tweeze answered 9/7, 2018 at 7:8 Comment(1)
This feature is known as lifecycle-hooks, you can refer for more info : kotest.io/docs/framework/lifecycle-hooks.htmlIives
P
13

Very similar to your own answer @JTeam, but use the init {} constructor block to declare your tests and then you can override methods directly in the class.

class MyTest : StringSpec() {

  override fun beforeTest(description: Description) {
    super.beforeTest(description)
    println("Before every spec/test case")
  }

  override fun beforeSpec(description: Description, spec: Spec) {
    super.beforeSpec(description, this)
    println("Before every test suite")
  }

  override fun afterTest(description: Description, result: TestResult) {
    super.afterTest(description, result)
    println("After every spec/test case")
  }

  override fun afterSpec(description: Description, spec: Spec) {
    super.afterSpec(description, spec)
    println("After every test suite")
  }

  init {
    "test should run " {
      "Hello".shouldHaveLength(4)
    }

    "test2 should run " {
      "Hello World".shouldHaveLength(10)
    }
  }
}
Prosthesis answered 15/7, 2018 at 7:46 Comment(0)
R
7

In newer versions of Kotest (I think that starting from 4.0.0) there are lifecylce functions on TestCofiguration and there's no need for the init block anymore:

import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.string.shouldHaveLength

class MyTest : StringSpec({
    beforeTest {
        println("Before every spec/test case")
    }

    beforeSpec {
        println("Before every test suite")
    }

    afterTest {
        println("After every spec/test case")
    }

    afterSpec {
        println("After every test suite")
    }

    "test 1 " {
        println("run test 1")
        "Hello" shouldHaveLength 4
    }

    "test 2 " {
        println("run test 2")
        "Hello World" shouldHaveLength 10
    }
})

More info

Rabb answered 17/11, 2021 at 10:19 Comment(0)
T
3

After doing some research in the github docs and kotlin-test framework source code, below is the code to write beforeTest, beforeSpec, afterTest, afterSpec

class MyTest : StringSpec({

    "test should run " {
        "Hello".shouldHaveLength(4)
    }

    "test2 should run " {
        "Hello World".shouldHaveLength(10)
    }
}) {
    override fun beforeTest(description: Description) {
        super.beforeTest(description)
        println("Before every spec/test case")
    }

    override fun beforeSpec(description: Description, spec: Spec) {
        super.beforeSpec(description, this)
        println("Before every test suite")
    }

    override fun afterTest(description: Description, result: TestResult) {
        super.afterTest(description, result)
        println("After every spec/test case")
    }

    override fun afterSpec(description: Description, spec: Spec) {
        super.afterSpec(description, spec)
        println("After every test suite")
    }
}

This is not looking elegant, if there is any way which can make it elegant, please post it.

Tweeze answered 9/7, 2018 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.