0

我有一个 kotlin kotest(以前称为 kotlintest)BehaviorSpec

在它下面有一个Given("...")和很多When("...") Then("...")

我想在整个规范(分别是每个Given子句)完成后执行清理。

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

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

在使用时,afterSpec { }我对子句进行了多次调用Whens??),afterSpec { }而不仅仅是在规范完成(或完成/每个Given子句)之后调用

在使用finalizeSpec { }它时根本不会被调用(它里面的断点永远不会被命中)

我究竟做错了什么?

还是我错过了BehaviorSpecs 的一些奇特特征?

4

1 回答 1

2

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/

于 2020-09-30T09:12:20.747 回答