This library has one purpose: to mimic Spock elegance in Kotlin JUnit 5 tests.
Look at this screenshot and decide if you want stick to parameterized tests in JUnit 5, or write tests with Spock elegance in Kotlin.
This library is in early development stage, API can be changed from version to version, documentation is not finished, bugs and not well described exception can happen.
- Parametrization with
where
table. Types of parameters are strictly checked.
@Spunit
fun test() = test { foo: Int?, bar: BigDecimal, expected: String? ->
// test code
}.where {
"foo" I "bar" II "expected"
1 I BigDecimal(2) II "3"
null I BigDecimal(1) II null
5 I BigDecimal(1) II "6"
}
-
given
,when
,then
,expect
blocks.
given("bar")
val bar = 1
`when`("baz = foo + bar")
val baz = foo?.let { it + bar }
then("baz == expected")
assertThat(baz?.toString()).isEqualTo(expected)
- Parametrization of test title.
@Spunit
fun `should run test with two parameters`() = test("#foo + 1 = #expected") { foo: Int?, expected: String? ->
// code
}.where {
"foo" II "expected"
1 II "2"
null II null
5 II "6"
}
Add one dependency, and you are good to go.
testImplementation group: 'pl.allegro.tech', name: 'spunit', version: version
A good introduction to this library is this test file.
- This lib is build on Junit 5 dynamic tests. Lifecycle of dynamic test is different from normal.
@BeforeEach
,@AfterEach
are only run once on start and end respectively. This problem is solved by Spunit lifecycle hooks (SpunitHooks
interface). - Sometimes title of parameterized test doesn't render parameters at all. There is an issue in gradle repo which tracks this problem gradle/gradle#5975.
- There is no formatter for where block, so autoformatting can make mess.
- Fix OneParamTestCase displayName
- Release as open-source software
Spunit is released under the Apache 2.0 license (see LICENSE)