Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write beforeEach and beforeClass in kotlintest

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")
  }
})
like image 862
JTeam Avatar asked Jul 09 '18 07:07

JTeam


3 Answers

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)
    }
  }
}
like image 61
sksamuel Avatar answered Nov 13 '22 09:11

sksamuel


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

like image 4
tibtof Avatar answered Nov 13 '22 09:11

tibtof


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.

like image 3
JTeam Avatar answered Nov 13 '22 07:11

JTeam