Does anyone know why this example does not seem to...
# kotest
a
Does anyone know why this example does not seem to work:
Copy code
class ContainerLifeCycleTest : FunSpec({

    val specName = ContainerLifeCycleTest::class.java.simpleName

    beforeSpec { println("before spec: $specName") }
    afterSpec { println("after spec: $specName") }

    context("outer context") {
        beforeContainer { println("before container: outer context") }
        afterContainer { println("after container: outer context") }
        beforeTest { test ->
            println("before test: ${test.name.testName}")
        }
        afterTest { (test, result) ->
            println(
                "after test: ${test.name.testName} (result: ${
                    result.isError.truth(
                        "failed",
                        "passed"
                    )
                })"
            )
        }
        test("outer context - test 1") { println("running: outer context 1 - test 1") }
        test("outer context - test 2") { println("running: outer context 1 - test 2") }
    }

})
I expect to see the following:
Copy code
before spec: ContainerLifeCycleTest
before test: outer context - test 1
before container: outer context
running: outer context 1 - test 1
after test: outer context - test 1 (result: passed)
before test: outer context - test 2
running: outer context 2 - test 2
after test: outer context - test 2 (result: passed)
after container: outer context
after spec: ContainerLifeCycleTest
But instead, I see that the
before
,
after
container hooks, are not called:
Copy code
before spec: ContainerLifeCycleTest
before test: outer context - test 1
running: outer context 1 - test 1
after test: outer context - test 1 (result: passed)
before test: outer context - test 2
running: outer context 2 - test 2
after test: outer context - test 2 (result: passed)
after spec: ContainerLifeCycleTest
What I’m a missing?
e
What happens if you put
beforeTest
inside the
test
? 🙂
a
I work it out in the end. The
beforeContainer {}
and
afterContainer {}
can only be used here:
Copy code
`
class ContainerLifeCycleTest : FunSpec({
  
  beforeSpec { "before spec ContainerLifeCycleTest" }
  afterSpec  { "after  spec ContainerLifeCycleTest" }

  // these callbacks only work at the root.
  // Moving them with in a "context" has no effect.
  // Counter intuitive ASFK! (e.g a context is not
  // a "container" of tests.
  beforeContainer { println("before container") }
  afterContainer { println("after container")}
 
  test("test-a")
  test("test-b")
  
  context("nested test") {
    test("nested-test-1") {}
    test("nested-test-2") {}
  }
})
`
Produces:
Copy code
before spec ContainerLifeCycleTest:

	before container:
	
	test-a
	test-b
	
	nested tests:
		nested-test-1
		nested-test-2
	
	after container

after spec ContainerLifeCycleTest: