All of the tests in my test suite need a specific ...
# testballoon
b
All of the tests in my test suite need a specific test-level fixture, so those tests are all inside a
testFixture
...
asParameterForEach
scope. One of my tests needs that fixture and also a second test-level fixture. Can I nest
testFixture
...
asParameterForEach
scopes? Or is that unwise? I see this example of nested fixtures. But it's not clear to me the inner test (
"test2"
) would be able to name and access the outer test fixture.
o
There is no mechanism to supply more that one test-level fixture to a test (although that fixture could have as many properties as you like with an
object
expression). With nested fixtures, a test would always receive the innermost one. Suite-level fixtures provide more flexibility if their
invoke
operator is used. Such suite-level fixture can be combined across multiple test suite levels. Is it just one test requiring a second fixture? If so, wouldn't a local property inside the test work?
b
> There is no mechanism to supply more that one test-level fixture to a test OK, thanks for confirming my impression. > Is it just one test requiring a second fixture? If so, wouldn't a local property inside the test work? Just one, at least for now. But the limitation we're discussing here is not going to trouble me much. I've made the fixture be an instance of a class that implements
AutoCloseable
, so a standard Kotlin
MyFixture().use { myFixture -> ... }
works perfectly in my test's body.
o
Thanks, that's good to hear!
🤝 1
b
I'll go even further. On more than one occasion, I've considered what "the TestBalloon way" of doing something would be and found that standard Kotlin features are already perfect for the task. That says good things about Kotlin's design and great things about how TestBalloon is designed to work with Kotlin instead of work around it. @Oliver.O, I tip my hat to you for TestBalloon's design: not only what that design includes, but also what that design lets you leave out. 🧠
o
Thank you so much, I really appreciate that! And yes, the small-surface API with good composability is meant to be one of TestBalloon's hallmarks. To be fair, it had the benefit of coming late to the party, building on a decade of Kotlin design experience unavailable to the earlier frameworks.
🏆 1
j
I'm also contemplating this because of how code would evolve. It seems like with this limitation, having test-level fixtures in higher level suites would be discouraged, because lower level suites might need to define their own later on. But also as the test evolves, what started as a leaf suite might eventually have several child suites of its own (and this is a touted feature of how TestBalloon handles parallelism, for example). This would require significant reorganization to fix. By contrast, if there was a TestFixture.PerTest.invoke() or something similar, then they could be composed the same way suite-scoped fixtures are composed.
o
I understand that composability and flexibility is very important, and if we find ways to push those to new limits, I'd appreciate it. During TestBalloon's initial fixture API design phase, test-level fixtures coming from multiple scopes were on the table. Actually, the Opensavvy Prepared library, which you can use on top of TestBalloon, offers these as "prepared values". Taking the example from there, it looks like this:
Copy code
val adminEmail by prepared { "my-account-${random.nextInt()}@mail.com" }

test("First test") {
    adminEmail() shouldEndWith "@mail.com"

    // Within a test, all calls are guaranteed to return the exact same value.
    adminEmail() shouldBe adminEmail()
}

test("Second test") {
    // This test will receive its own value
    // that is shared with no other test.
    adminEmail() shouldEndWith "@mail.com"
}
That mechanism would provide more flexibility than TestBalloon's own test-level fixtures. So why wasn't this implemented in TestBalloon itself? The answer is comprehensibility, which is one of TestBalloon's major design goals. TestBalloon's fixtures return predictable values within their scope (for test-level fixtures it's the test, for suite-level fixture it's the suite). Their syntactical elements (parameters, receivers) make that clear in plain Kotlin. There is no magic involved. With Prepared, the above
adminEmail()
function keeps or changes its result depending on where it is invoked (first time in a test, subsequently in the same test). There is no syntactical element binding the prepared value to a scope. It is magic which has to be learned. That said: Maybe we can find a way to combine the best of both worlds. And as always with every new feature: For a good start we need concrete real-world use cases, which would be too cumbersome to handle now. Once we have that, we can explore the boundaries of existing solutions and can better judge the upsides and downsides of possible future solutions. Suggestions welcome!
🧠 1
🤔 1