Skip to main content
Version: 5.8

Spec Lifecycle and Extensions

Kotest has an extensive selection of hooks through which you can integrate into the spec lifecycle.

Lifecycle
A spec is selected for execution by the TestSuiteScheduler and a coroutine is created for the spec.
Any SpecLaunchExtensions are invoked, passing in the reference to the spec that has been scheduled. The extensions may opt to skip execution or continue. Any changes to the coroutine context are propagated downstream.
If either the spec is annotated with @Ignored or the spec is annotated with @EnabledIf and fails the if condition, then the spec will be skipped. If skipped, any SpecIgnoredListener are invoked with the reference to the spec.
If the spec is not skipped, an instance of the spec is created. On the JVM this process will delegate to any ConstructorExtensions that are registered, or if none exist, then the default instantiation method is used (reflection). On other platforms the spec will be created directly.
On the JVM, the instantiated spec will be passed to any PostInstantiationExtensions which have the ability to adjust the instance (for example, applying dependency injection).
If spec creation is successful, then any SpecInitializeExtensions are invoked and if spec creation fails, then any SpecCreationErrorListeners are invoked with the exception.
Any SpecInterceptExtensions are invoked passing in the created spec. These extensions may opt to skip execution or continue. Any changes to the coroutine context are propagated downstream.
If the spec is active (contains at least one, enabled, root test case), then any PrepareSpecListeners are invoked. Otherwise, the spec is inactive, and any InactiveSpecListeners are invoked and execution is skipped.
Any BeforeSpecListeners are invoked passing in the spec instance. Any errors in these extensions will cause test execution and after spec listeners to be skipped.
All tests in the spec are executed.
Any AfterSpecListeners are invoked passing in the spec instance.
Any FinalizeSpecListeners are invoked passing in the KClass reference to the spec that was completed.

Note: For each isolated spec, a fresh spec instance will be created and the PostInstantiationExtension, SpecCreatedListener, SpecCreationErrorListener, SpecInterceptExtension, BeforeSpecListener and AfterSpecListener callbacks will be repeated.