https://kotlinlang.org logo
Title
j

James Eschner

04/01/2021, 4:50 PM
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:
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:
~~~ 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

sam

04/01/2021, 7:29 PM
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

James Eschner

04/01/2021, 10:59 PM
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

sam

04/01/2021, 11:33 PM
so like a beforeFactory kind of thing
l

LeoColman

04/02/2021, 6:59 AM
beforeGroup
j

James Eschner

04/02/2021, 1:05 PM
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

christophsturm

04/02/2021, 3:14 PM
why do your messages all say * not a contribution * at the bottom?
s

sam

04/02/2021, 3:15 PM
I was wondering that too 🙂
Here is the ticket for beforeGroup: https://github.com/kotest/kotest/issues/2188
j

James Eschner

04/02/2021, 3:42 PM
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 *