(NO LONGER MAINTANED) KSpec -> Spek.
Specifications for Kotlin.
class TheMostAmazingAnimalSpec: KSpec() {
override fun spec() {
describe("the most amazing animal in the universe") {
val animal = GetMostAmazingAnimal()
it("should be a panda") {
assertThat(animal.type, equalTo("panda"))
}
context("not a panda") {
it("nope, not going to accept it") {
assertThat({
assertThat(animal.type, not(equalTo("panda")))
}, thrown(EndOfTheWorldException::class))
}
}
}
}
}
KSpec is heavily inspired by RSpec, Context
is synonymous to RSpec's scopes.
The test method in JUnit and created using it
.
Groups similar examples together (they might be testing the same Subject
- more on this later) and is created by using describe
or context
. Be cautious in placing logic code in them as they are eagerly evaluated
KSpec provides before
, beforeEach
, afterEach
and after
callbacks for each context.
Just like RSpec, KSpec also support subjects.
class TheMostAmazingAnimalSpec: KSpec() {
override fun spec() {
describe("the most amazing animal in the universe") {
subject {
return@subject GetMostAmazingAnimal();
}
it("should be a panda") {
assertThat(subject.type, equalTo("panda"))
}
context("not a panda") {
it("nope, not going to accept it") {
assertThat({
assertThat(subject.type, not(equalTo("panda")))
}, thrown(EndOfTheWorldException::class))
}
}
}
}
}
Sometimes it's convenient to reuse examples - like testing a subclass.
class CalculatorSpec: KSpec() {
override fun spec() {
describe(Calculator::class) {
itBehavesLike(calculator())
}
}
companion object {
fun calculator() = sharedExample<Calculator> {
describe("add") {
it("1 + 1 = 2") {
assertThat(subject.add(1, 1), equalTo(2))
}
}
...
}
}
}
class AdvancedCalculatorSpec: KSpec() {
override fun spec() {
describe(AdvancedCalculator::class) {
itBehavesLike(CalculatorSpec.calculator())
}
}
}
You can write specs in advance, KSpec will ignore them during execution.
class SomeSpec: KSpec() {
override fun spec() {
xdescribe("a pending group") {
it("won't be executed") { }
}
xcontext("another pending group", "some reason")
xit("a pending example") { }
}
}
KSpec supports focusing execution only on several contexts. Use fdescribe
and fcontext
to create a focused group, and fit
to create a focused example. KSpec will only run focused contexts if there are any, othewise it will run everything.
TODO
This allows to control which contexts to run. It can be configured per spec (by overriding configure
) or by using Shared Configurations.
class SomeSpec: KSpec() {
override fun configure(config: KSpecConfig) {
// config.filter.<filter> = tags
}
}
Only include contexts having at least one of the tags
specified.
Exclude any contexts having at least one of the tags
specified.
Similar to the include filter, the only difference is that if there is no match run everything.
Declare shared configurations by extending Configuration
and apply it to a spec via @Configurations
.
class SharedConfiguration: Configuration {
override fun apply(config: KSpecConfig) {
...
}
}
class AnotherConfiguration: Configuration {
override fun apply(config: KSpecConfig) {
...
}
}
// use it
@Configurations(
SharedConfiguration::class,
AnotherConfiguration::class
)
class SomeSpec: KSpec() {
...
}
TODO
Currently you need to use a JUnit4 Runner
to be able to run specs with gradle. Make sure to annotate your test classes with @RunWith(JUnitKSpecRunner::class)
.
repositories {
jcenter()
}
dependencies {
testCompile "io.polymorphicpanda.kspec:kspec-core:<kspec-version>"
testCompile "io.polymorphicpanda.kspec:kspec-junit-runner:<kspec-version>"
}
repositories {
maven {
url "http://oss.jfrog.org/artifactory/oss-snapshot-local/"
}
}