Dirk Hoffmann
Dirk Hoffmann

Reputation: 1573

kotlin kotest/kotlintest BehaviorSpec afterSpec/finalizeSpec called too often or not at all

I have a kotlin kotest (formerly known as kotlintest) BehaviorSpec

with one Given("...") and many When("...") Then("...") under it

I want to execute a cleanup after the whole Spec (respectively every Given clause) has finished.

    @MicronautTest
    class StructurePersistSpec(
            private val iC : InstancesC
    ) : BehaviorSpec({
    
        // afterSpec {
        finalizeSpec {
            cleanup()
        }

        Given("...") {
            When("...") {
                Then("...") {
                ...
                }
                Then("...") {
                ...
                }
            }
            When("...") {
                Then("...") {
                ...
                }
                Then("...") {
                ...
                }
            }
        }   
   ...
   }

on using afterSpec { } I get multiple calls (amount of Whens??) to the afterSpec { } clause and NOT just one after the Spec finished (or finishing of the/each Given Clause)

on using finalizeSpec { } it does NOT get called at all (breakpoint inside it is never hit)

what am I doing wrong?

or did I miss some fancy characteristics of BehaviorSpecs ?

Upvotes: 0

Views: 1709

Answers (1)

Alex Facciorusso
Alex Facciorusso

Reputation: 2408

The reason you are getting multiple calls is that probably you have set a different IsolationMode for your test.

That would mean your Spec will be recreated (and then cleaned) for every test. In order to have a single afterSpec call from the framework, your IsolationMode must be set to SingleInstance.

Bare in mind that might affect the way your tests are being executed hence their validity or ability to pass.

Documentation: https://kotest.io/isolation_mode/

Upvotes: 2

Related Questions