I'm working with Kotlin Multiplatform in a multi-module setup. I want to create a dedicated module that defines reusable test setup and teardown logic—similar to JUnit rules—in the commonMain source set. This module should be consumed by other modules via their respective commonTest source sets to avoid duplication.
The goal is for each module to seamlessly reuse the shared setup logic by adding this common module as a test dependency.
What I'm Trying to Achieve
- Define shared test hooks using
@BeforeTestand@AfterTestin thecommonMainsource set. - Reuse this setup logic across multiple modules.
- Allow each module to easily consume the shared logic in their
commonTestsource sets.
The Issue
Although the IDE correctly resolves the kotlin.test annotations in the commonMain source set, the test runner fails to recognize them during execution. Specifically, the @BeforeTest and @AfterTest annotations from the kotlin.test package are not recognized when running tests.
My Question
What is the recommended way to define and share reusable @BeforeTest and @AfterTest hooks from commonMain so that they can be reliably used in commonTest across multiple modules?
How can I structure this setup to ensure proper test execution, reusability, and maintainability?
Any examples, insights, or best practices would be greatly appreciated!
@BeforeTestsomehwere else? But I assume you want the@BeforeHookto be automatically executed and not have the annotation present in the test anymore?commonTestset.