Spek is a BDD style test framework for Kotlin which includes the following high-level features:
- Arbitrarily nestable
describe
blocks for grouping tests with a shared context (also available under the aliasescontext
,given
, andon
) - Setup and teardown using
beforeEach
andafterEach
blocks, which will be run before/after eachit
in thedescribe
- The ability to mark tests as pending by using an
x
- prefix (e.g.xdescribe
) - The ability to run only a subset of "focused" tests by using an
f
- prefix (e.g.fit
) - Integration with Junit (and IntelliJ via IntelliJ's Junit integration)
- A standalone, junit-independent console runner
Add the following line to your build.gradle
testCompile 'org.jetbrains.spek:spek:1.0.0'
- Build by running
./gradlew installApp
- The executable script will be found at
spek-dist/build/install/spek/bin/spek
- To invoke the console runner, you need both the “path” and the “package” arguments. The “path” should only be the part of the path up until it matches the package path. For example:
spek-dist/build/install/spek/bin/spek spek-samples/build/classes/main org.jetbrains.spek.samples
class ExampleTest : Spek({
describe("a calculator") {
val calculator = SampleCalculator()
var result = 0
describe("addition") {
beforeEach {
result = calculator.sum(2, 4);
}
it("should return the result of adding the first number to the second number") {
assertEquals(6, result)
}
it("should fail") {
assertEquals(7, result)
}
}
describe("subtraction") {
beforeEach {
result = calculator.subtract(4, 2)
}
it("should return the result of subtracting the second number from the first number") {
assertEquals(2, result)
}
}
}
})
Please see the samples folder for more examples