dave08
11/05/2025, 10:53 AMOliver.O
11/05/2025, 11:06 AMdave08
11/05/2025, 11:30 AMOliver.O
11/05/2025, 11:38 AMdave08
11/05/2025, 12:25 PMdave08
11/05/2025, 12:26 PMOliver.O
11/05/2025, 12:51 PMdave08
11/05/2025, 1:02 PMdave08
11/05/2025, 1:03 PMdave08
11/05/2025, 1:04 PMOliver.O
11/05/2025, 1:15 PMdave08
11/05/2025, 1:25 PMclass PlayerServiceImplTest {
lateinit var fakeEngine: FakePlayerEngine
lateinit var playerService: PlayerServiceImpl
@OptIn(ExperimentalCoroutinesApi::class)
@BeforeEach
fun setup() {
Dispatchers.setMain(Dispatchers.Unconfined)
fakeEngine = FakePlayerEngine()
playerService = PlayerServiceImpl(fakeEngine)
}
@Test
fun `setting new items to queue should overwrite the current queue`() = runTest {
playerService.currentQueue.test {
assertEquals(PlayerQueue.EMPTY, awaitItem())
playerService.setQueue(streams)
assertEquals(PlayerQueue(streams, 0), awaitItem())
playerService.setQueue(streams2)
assertEquals(PlayerQueue(streams2, 0), awaitItem())
}
assertEquals(streams2, fakeEngine._currentStreams)
}
...dave08
11/05/2025, 1:38 PMOliver.O
11/05/2025, 2:31 PMOliver.O
11/05/2025, 4:47 PM@OptIn(TestBalloonExperimentalApi::class) // for TestConfig.mainDispatcher
val PlayerServiceImplTestBalloonTest by testSuite(
testConfig = TestConfig.mainDispatcher(Dispatchers.Unconfined)
) {
class Context {
val fakeEngine = FakePlayerEngine()
val playerService = PlayerServiceImpl(fakeEngine)
}
@TestRegistering
fun test(name: String, action: suspend Context.() -> Unit) =
this.test(name) { Context().action() }
test("setting new items to queue should overwrite the current queue") {
playerService.currentQueue.test {
assertEquals(PlayerQueue.EMPTY, awaitItem())
playerService.setQueue(streams)
assertEquals(PlayerQueue(streams, 0), awaitItem())
playerService.setQueue(streams2)
assertEquals(PlayerQueue(streams2, 0), awaitItem())
}
assertEquals(streams2, fakeEngine._currentStreams)
}
}
So there's no "resetting", no re-instantiation magic behind the scenes. Just initializing the Context in plain Kotlin. And you can literally see that it's done for each test. What do you think?dave08
11/05/2025, 4:51 PMOliver.O
11/05/2025, 5:07 PMtestFixture different and how does it relate to the above?
• The testFixture is scoped to its suite and initialized only once. Multiple calls return the same value, so it's easy to reason about it.
• If you'd like a testFixture to create a suite-specific context, you'd also have to define a class.
• Of course, you can have multiple `testFixture`s, but then you'd have to remember invoking each of them.
I have indeed considered a testFixtureForEach, but the downside would be the magic behind the scenes. It's a stealth setup with side-effects that no longer communicates accurately what happens:
• Inside a test, the first invocation of testFixtureForEach() would create a new object.
• Afterwards, in the same test, each invocation of testFixtureForEach() would return the same object.
So in that respect, I agree with what's excellently explained in detail in The Quality of Test:
If brevity compromises accurate readability, then you must throw the brevity out.Does that make sense?
Bernd Prünster
11/05/2025, 5:18 PMaroundEach. Not super clean.
The next part is all very brainstormy, BUT: Given TestBalloon's flexibility, you could probably add some magic with refied generics as an extension on a generator function and magically plug that into your test definitions via context parameters, so the generator gets called automagically AND you don't have to explicitly specify it (thanks to context parameters, if you've declared it before your first test)dave08
11/05/2025, 7:15 PMBernd Prünster
11/05/2025, 7:21 PMcontext(suite: TestSuite)
inline fun <reified T> (suspend () -> T).Test(
name: String,
testConfig: TestConfig = TestConfig,
crossinline nested: suspend TestExecutionScope.(T) -> Unit
) {
suite.test(name, testConfig = testConfig) { nested(this@Test()) }
}
context(suite: TestSuite)
inline fun <reified T> (() -> T).Test(
name: String,
testConfig: TestConfig = TestConfig,
crossinline nested: suspend TestExecutionScope.(T) -> Unit
) {
suite.test(name, testConfig = testConfig) { nested(this@Test()) }
}
val aGeneratingSuite by testSuite {
{ Random.nextBytes(32).toHexString() }.apply {
listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).forEach {
Test("$it") { freshFixture ->
//will always fail with fresh randomness for each invocation
freshFixture shouldBe "Hello"
}
}
}
}Bernd Prünster
11/05/2025, 8:56 PMdave08
11/05/2025, 11:43 PMBernd Prünster
11/06/2025, 5:56 AMaroundEach, but that only worked by using shared state, which is something you really don't want. Essentially, ye olde JUnit4 test style that prevents concurrency and leaks state all over the place. I'll be sugar-coating my experiment a litte bit to make the keywords a bit more expressive. In the end though: as long as your generator function does not introduce side effects you can then test concurrently, you can nest arbitrarily and the scope is clear. It essentially takes what was boilerplate in Oliver's sketch, and generalises it to make it re-usable. But yeah, it's a programming trick. Or rather: TestBalloon allows for such patterns to emerge. And what more could you want from a solid foundation for multiplatform testing?! For us, at least, migrating from Kotest was transformative and unearthed some really nasty bugs that slipped through the old test suite.
As for green and blue being magic: Not having that distinction is a far bigger problem, if you think about what that would mean for nesting tests and running only specific tests some layers down. Just does not compute…Bernd Prünster
11/06/2025, 8:35 AMdave08
11/06/2025, 12:14 PMdave08
11/06/2025, 12:15 PMdave08
11/06/2025, 12:16 PMBernd Prünster
11/06/2025, 12:19 PMOliver.O
11/06/2025, 12:33 PMtestFixtures to be accurately readable as they are scoped to their declaration and retain their value. Test-scoped fixtures are not: They would be accessible within the scope they are declared in, but change their value between tests, but not inside a test. You have to learn this behavior, it's not clear from reading the code.
As mentioned above, tests should accurately communicate what they do. So we have different priorities here, and it's not carved in stone. If many people would prefer that style, I can always add a test-scoped fixture later. But I don't want to rush and accidentally offer a risky API that might lead to unhealthy patterns in user code. If you want it now, you can always build your own extension for it or just use Prepared.
TestBalloon invites to to pick your own approach, as it is really easy to extend and adapt to your needs. And in addition, Prepare builds on TestBalloon, so you can cherry-pick what works best for you. I think that's a good thing that TestBalloon users are always free to choose what works best for them.dave08
11/06/2025, 12:38 PMdave08
11/06/2025, 12:43 PMBernd Prünster
11/06/2025, 12:45 PMaroundEach and enforce sequential test execution.
granted, three LoC more than JUnit, but it behaves the same, including all the pitfalls. We use that sill for tests we were to lazy to properly migrate with no issuesdave08
11/06/2025, 12:46 PMBernd Prünster
11/06/2025, 12:46 PMdave08
11/06/2025, 12:46 PMdave08
11/06/2025, 12:48 PMBernd Prünster
11/06/2025, 12:49 PMdave08
11/06/2025, 12:49 PMBernd Prünster
11/06/2025, 12:52 PMBernd Prünster
11/06/2025, 12:54 PMdave08
11/06/2025, 12:59 PMBernd Prünster
11/06/2025, 1:00 PMdave08
11/06/2025, 1:00 PMdave08
11/06/2025, 1:01 PMdave08
11/06/2025, 1:01 PMOliver.O
11/06/2025, 1:15 PMpeople are so used to this pattern in junit that it's pretty much expected.I am aware of this. But sometimes you have to say good by to dinosaur testing patterns if they do more harm than good. Yes, the class+helper costs a few lines of code. But these amortize among lots of tests, and your plain Kotlin skills tell you what's going on.
Getting into the blue-green areaThat's not specific to TestBalloon or testing (I just tried to make people aware of the situation): It's what happens in languages with higher-order functions. You pass a function that's called later. It's everywhere in Kotlin and almost every modern language, so it's pretty much unavoidable magic that makes us way more productive.
even when I used kotest, I mostly used the one class instance per test mode and declared my vals inside the suite blocks (wherever a state applied to a whole suite), but I still expected it to reset after each test.Yes, Kotest provided that with
InstancePerTest isolation mode. And learned the hard way that it brought lots of complications with their DSL and never worked correctly across use cases. So now, Kotest deprecates InstancePerTest and directs users to use InstancePerRoot, which is even less intuitive, and everything is still JVM-only. So that's a mess with footguns of various degrees. If you look at the number of Kotest issues dealing with when before* and after* blocks get called, you might imagine that it's something TestBalloon doesn't want to replicate.
So yes, you have to learn a new pattern to be safe and multiplatform. Sometimes we just have to, otherwise we all would still be writing COBOL (and never achieve what we can do today).dave08
11/06/2025, 1:31 PMdave08
11/06/2025, 1:32 PMOliver.O
11/06/2025, 1:55 PMdave08
11/06/2025, 1:56 PMdave08
11/06/2025, 1:58 PMOliver.O
11/06/2025, 1:58 PMdave08
11/06/2025, 2:01 PMOliver.O
11/06/2025, 2:13 PMtestBalloon {
@OptIn(TestBalloonInternalApi::class)
debugLevel = DebugLevel.DISCOVERY
}
Then you should see something like this as compilation warnings:
w: ...build/generated/testBalloon/src/commonTest/kotlin/EntryPointAnchor.kt:1:1 Plugin de.infix.testBalloon: [DEBUG] Analyzing source file
w: ...src/commonTest/kotlin/com/example/MyFirstTestSuite.kt:1:1 Plugin de.infix.testBalloon: [DEBUG] Analyzing source file
w: ...src/commonTest/kotlin/com/example/MyFirstTestSuite.kt:6:1 Plugin de.infix.testBalloon: [DEBUG] Found test discoverable 'com.example.MyFirstTestSuite'dave08
11/06/2025, 2:17 PMdave08
11/06/2025, 2:18 PMOliver.O
11/06/2025, 2:19 PMPlayerServiceExtraFeaturesTest?dave08
11/06/2025, 2:19 PMOliver.O
11/06/2025, 2:20 PMdave08
11/06/2025, 2:20 PMOliver.O
11/06/2025, 2:21 PMOliver.O
11/06/2025, 2:22 PMdave08
11/06/2025, 2:22 PMtestBalloon = { id = "de.infix.testBalloon", version.ref = "testBalloon" }
testBalloon = "0.7.1-K2.2.21"Oliver.O
11/06/2025, 2:23 PMdave08
11/06/2025, 2:23 PMdave08
11/06/2025, 2:24 PMandroid-kotlin-multiplatform-library = { id = "com.android.kotlin.multiplatform.library", version.ref = "agp" }
agp = "8.12.3"
junit5 vintage works (which is junit5's junit 4 adapter)dave08
11/06/2025, 2:24 PMdave08
11/06/2025, 2:25 PMgetByName("androidHostTest") {
dependencies {
implementation(libs.androidx.junit.ktx)
if (project.findProperty("local.androidHostTestDance") != "removeDependency") {
implementation(libs.testBalloon.core)
implementation(libs.turbine)
implementation(libs.robolectric)
implementation(libs.junit.vintage.engine)
implementation(kotlin("test"))
implementation(libs.androidx.test.core)
implementation(libs.mokkery.core)
}
}
}Oliver.O
11/06/2025, 2:29 PMdave08
11/06/2025, 2:56 PMdave08
11/06/2025, 2:57 PMdave08
11/06/2025, 2:57 PMOliver.O
11/06/2025, 3:03 PM--tests parameter and supplying TestBalloon's test element path (what's now in TESTBALLOON_INCLUDE_PATTERNS). You can safely replace the south-west arrow with a pipe | and see how that goes. If that doesn't work, try a star * right after the top-level suite name. But I'm not sure we can make every combination work: Gradle is just too opinionated, and has trouble with non class/method-based organizations. This is even true for JUnit 5 itself: It can do multi-level hierarchies, but Gradle's reporting cannot.Oliver.O
11/06/2025, 3:38 PMtasks.withType<Test>().configureEach {
useJUnitPlatform {
if (System.getenv("TESTBALLOON_INCLUDE_PATTERNS") != null) {
excludeEngines("junit-vintage")
}
}
}
Does it?dave08
11/06/2025, 4:01 PMdave08
11/06/2025, 4:03 PM