CLOVIS
02/11/2023, 10:17 AMAdam S
02/11/2023, 10:39 AM@Test
fun myTest() {
val myService = MyService()
fun testService(expected: String, data1: Int, data2: Int) {
val actual = myService.someOperation(data1, data2)
assertEquals(expected, actual)
}
testService("blah 1 2", 1, 2)
testService("blah 2 2", 2, 2)
testService("blah 3 2", 3, 2)
testService("blah 4 2", 4, 2)
}
It’s not ideal because it doesn’t create separate tests in the reports, and the tests fail early, but it works well enough. It’s used a lot in Kotlinx Serialization tests.CLOVIS
02/11/2023, 10:40 AMthe tests fail earlyThat's the main thing I dislike
Adam S
02/11/2023, 10:43 AMCLOVIS
02/11/2023, 10:47 AMinterface
that share their test cases (to ensure they all respect the interface's contract). What I'm currently doing is
// Module test
abstract class FooTestCases<T : Foo> {
abstract suspend fun new(): T
@Test
fun create() = runTest {
val foo = new()
// test…
}
}
// For each impl…
class FooImplTest : FooTestCases<FooImpl> {
override fun new() = FooImpl(…)
}
This approach worked well (tests do not fail early, there is a proper test suite per implementation with proper naming etc); however it fails spectacularly when some objects have dependencies because the abstract class and implementation both initialize different objects.
I was considering Kotest because it would allow me to declare something like (have not used it yet so syntax may be wrong, but from the documentation this should be possible):
fun TestCase.testFooContract(new: () -> Foo) {
"create" - {
val foo = new()
// …
}
// …
}
class Impl1 : StringSpec({
testFooContract { FooImpl1(…) }
})
class Impl2 : StringSpec({
testFooContract { FooImpl2(…) }
})
which is much more ergonomic.CLOVIS
02/11/2023, 10:49 AMAdam S
02/11/2023, 10:51 AMAdam S
02/11/2023, 10:53 AMAdam S
02/11/2023, 10:54 AM@JsModule("kotlin-test")
external val kTest:dynamic
kTest.kotlin.test.suite("package test", false){
kTest.kotlin.test.suite("suit test", false) {
kTest.kotlin.test.test("test", false) {
assertTrue(true)
}
}
}
CLOVIS
02/11/2023, 10:56 AMCLOVIS
02/11/2023, 10:56 AMAdam S
02/11/2023, 10:57 AMAdam S
02/11/2023, 10:57 AMCLOVIS
02/11/2023, 10:57 AMAdam S
02/11/2023, 11:01 AMCLOVIS
02/11/2023, 11:01 AMCLOVIS
02/11/2023, 11:01 AMAdam S
02/11/2023, 11:02 AMCLOVIS
02/11/2023, 11:06 AMbackgroundScope
) that would take even more time to get added.Adam S
02/11/2023, 11:44 AMAdam S
02/11/2023, 11:44 AMCLOVIS
02/11/2023, 11:46 AMCLOVIS
02/11/2023, 11:47 AMAdam S
02/11/2023, 11:47 AMAdam S
02/11/2023, 11:48 AMAdam S
02/11/2023, 11:48 AMsuspend
but I think that it’s fineCLOVIS
02/11/2023, 11:50 AMAdam S
02/11/2023, 11:50 AMCLOVIS
02/11/2023, 11:51 AMAdam S
02/11/2023, 11:51 AMAdam S
02/11/2023, 11:52 AMCLOVIS
02/11/2023, 11:53 AMCLOVIS
02/11/2023, 11:54 AMAdam S
02/11/2023, 11:54 AMCLOVIS
02/11/2023, 1:03 PMkotlin-test-junit
to kotlin-test-junit5
. I thought it would be as simple as replacing the dependency, but now the JVM tests do not run anymore.CLOVIS
02/11/2023, 1:03 PMAdam S
02/11/2023, 1:03 PMuseJUnitPlatform()
in Gradle?CLOVIS
02/11/2023, 1:03 PMAdam S
02/11/2023, 1:04 PMuseJUnitPlatform()
the tests will run but you won’t get any console/report output