mike-neckのブログ

Java or Groovy or Swift or Golang

kotlintest の Listener の実行順番

わからなかったので、実験した。コードを読んだわけではない。

f:id:mike_neck:20171113232305p:plain


実験コード

class TestTest: BehaviorSpec({
  given("given-1") {
    `when`("when-1") {
      then("then-1") {
        println("given-1/when-1/then-1")
      }
      then("then-2") {
        println("given-1/when-1/then-2")
      }
    }
    `when`("when-2") {
      then("then-1") {
        println("given-1/when-2/then-1/print")
      }
    }
  }
  given("given-2") {
    `when`("when-1") {
      then("then-1") {
        println("given-2/when-1/then-1/print")
      }
    }
  }
}) {
  override fun listeners(): List<TestListener> = listOf(object : TestListener {
    override fun afterTest(testCase: TestCase, result: TestResult) = println("after: test-case: ${testCase.name}/ result: ${result.status}")
    override fun beforeTest(testCase: TestCase) = println("before: test-case: ${testCase.name}")
    override fun afterProject() = println("after-project")
    override fun beforeProject() = println("before-project")
    override fun afterSpec(spec: Spec) = println("after-spec: ${spec.description().name}(${spec.description().depth()})")
    override fun beforeSpec(spec: Spec) = println("before-spec: ${spec.description().name}(${spec.description().depth()})")
  })
}

実行結果

before-spec: com.example.book.infra.TestTest(1)
before: test-case: Given: given-1
before: test-case: When: when-1
before: test-case: Then: then-1
given-1/when-1/then-1
after: test-case: Then: then-1/ result: Success
before: test-case: Then: then-2
given-1/when-1/then-2
after: test-case: Then: then-2/ result: Success
after: test-case: When: when-1/ result: Success
before: test-case: When: when-2
before: test-case: Then: then-1
given-1/when-2/then-1/print
after: test-case: Then: then-1/ result: Success
after: test-case: When: when-2/ result: Success
after: test-case: Given: given-1/ result: Success
before: test-case: Given: given-2
before: test-case: When: when-1
before: test-case: Then: then-1
given-2/when-1/then-1/print
after: test-case: Then: then-1/ result: Success
after: test-case: When: when-1/ result: Success
after: test-case: Given: given-2/ result: Success
after-spec: com.example.book.infra.TestTest(1)