Hello, I’m using a kotest `TestFactory` and trying...
# kotest
j
Hello, I’m using a kotest 
TestFactory
 and trying to understand the lifecycle. It appears that the 
beforeTest
 /`afterTest` functions are executed but not the `beforeSpec`/`afterSpec` functions. Can someone help me understand why? Code:
Copy code
val myTestFactory: TestFactory = shouldSpec {
    beforeSpec {
        println("beforeSpec")
    }

    beforeTest {
        println("beforeTest")
    }

    afterSpec {
        println("afterSpec")
    }

    afterTest {
        println("afterTest")
    }

    should("help me understand the factory lifecycle") {
        println("during")
    }
}

class MyTest : ShouldSpec({
    include(myTestFactory)
})
Output:
Copy code
~~~ Kotest Configuration ~~~
-> Parallelization factor: 1
-> Default test timeout: 600000ms
-> Default test order: Sequential
-> Default isolation mode: SingleInstance
-> Global soft assertations: False
-> Write spec failure file: False
-> Fail on ignored tests: False
-> Spec execution order: SpecExecutionOrder
-> Extensions
  - io.kotest.engine.extensions.SystemPropertyTagExtension
  - io.kotest.core.extensions.RuntimeTagExtension
  - io.kotest.engine.extensions.RuntimeTagExpressionExtension

beforeTest
during
afterTest
MyTest > should help me understand the factory lifecycle PASSED
* Not a Contribution *
s
I guess the before spec can't work, as the spec already exists before the factory methods are called. So maybe we should remove that from test factory.
j
Since a
TestFactory
can have more than one test in it it makes sense for it to have a lifecycle that’s similar to a regular spec. What I mean is that there should be facilities to run arbitrary code before and after each test as well as before and after all tests. * Not a Contribution *
s
so like a beforeFactory kind of thing
l
beforeGroup
j
Sure,
beforeGroup
or
beforeFactory
both seem reasonable. This would be awesome to have, it took me some time to figure out what was going wrong. * Not a Contribution *
c
why do your messages all say * not a contribution * at the bottom?
s
I was wondering that too 🙂
Here is the ticket for beforeGroup: https://github.com/kotest/kotest/issues/2188
j
One argument for keeping the name `beforeSpec`/`afterSpec` would be so that you could apply a
TestListener
that implements `beforeSpec`/`afterSpec` inside of a
TestFactory
* Not a Contribution *