Grouping Tests with Tags
Sometimes you don't want to run all tests and Kotest provides tags to be able to determine which
tests are executed at runtime. Tags are objects inheriting from io.kotest.core.Tag.
For example, to group tests by operating system you could define the following tags:
object Linux : Tag()
object Windows : Tag()
Alternatively, tags can be defined using the NamedTag class. When using this class, observe the following rules:
- A tag must not be null or blank.
- A tag must not contain whitespace.
- A tag must not contain ISO control characters.
- A tag must not contain any of the following characters:
- !: exclamation mark
- (: left paren
- ): right paren
- &: ampersand
- |: pipe
For example:
val tag = NamedTag("Linux")
If two tag objects have the same simple name (even in different packages) they are treated as the same tag.
Adding tags to tests​
Tagging Tests​
Tests can be tagged at various levels. Firstly, test cases themselves can have tags added via test config.
Note that any nested tags inherit tags from their parents.
class MyTest : FunSpec() {
init {
test("should run on Windows").config(tags = setOf(Windows)) {
// ...
}
test("should run on Linux").config(tags = setOf(Linux)) {
// ...
}
context("should run on Windows and Linux").config(tags = setOf(Windows, Linux)) {
test("and nested tests") { // implicity has windows and linux tags added
}
}
}
}
Tagging Specs​
Secondly, you can add tags at the spec level, either through the tags function in the spec itself, or through the @Tags annotation. Any tags added this way are applied to all tests implicitly.
When tagging tests in this way, the spec class will still need to be instantiated in order to examine the tags on each test, because the test itself may define further tags. Therefore, do not rely on this if you want to avoid instantiating classes completely, and instead see @RequiresTag.
@Tags("Foo") // applied to all tests in this spec
private class TaggedSpec : ExpectSpec() {
init {
tags(Windows) // applied to all tests in this spec
expect("should run on Windows") {
// ...
}
}
}
Any tags added via @Tags do not stop the spec from being instantiated, as the engine needs to check for any tags added via code. If you want to avoid a spec from being instantiated completely, use @RequiresTag.