Within kotlin.test, there are a number of annotations that can be used to mark our test classes and functions in a test framework-independent fashion. Examples of these annotations include the following:
- @AfterClass: Indicates that a function should be run after a test suite
- @AfterTest: Indicates that a function should be run after each test
- @BeforeTest: Indicates that a function should be run before each test
- @Test: Marks a function as a test
These annotations may look familiar if you've used other test frameworks in the past. These annotations are meant to provide similar functionality in a more native way. The specific meaning of each annotation can then be customized for whichever test framework you're targeting.
In this example, we're targeting kotlin-test-junit and using the @BeforeTest annotation to create a function that will run before any of our defined Junit tests:
class UtilitiesKtTest {
@BeforeTest
fun runBeforeAnyTest() {
println("before test")
}
...
}
Once the annotation has been added, you can then swap out the underlying kotlin-test target if you need to switch to a different testing framework, or if you need to target a different framework when working in a multi-platform project.