Is it possible to do a test fixture independent of...
# testballoon
e
Is it possible to do a test fixture independent of the test suite context? An example here is the consolidated DB tests github.com/emartynov/nowinandroid/commit/60f71b3f4b3a…#… On the one hand, the original tests were done as separate tests: github.com/emartynov/nowinandroid/blob/…/DatabaseTest.kt, so I was thinking if I could separate the test fixture and reuse it in two different suites.
✅ 1
Ah, should be easy-peasy. I'm thinking about something like
val DB = testFixture{}
but should be
fun TestScope.DBFixture() = testFixture{}
👍 1
I'm sorry for staring asking in slack before deeper thinking around it.
o
Don’t worry. Nobody is forced to answer right away and it may be interesting for others to follow your thoughts on the way to a resolution.
thank you color 1
➕ 1
e
I like having DB test in one place but reading that file becomes a burden
This is probably a first personal opinion/feedback that reading junit tests are lighter probably because of less nesting. But that should be solvable.
o
I was already measuring the visual footprint of code and haven’t found a case where JUnit was lighter so I’m curious to see actual examples where that happens. 👀
🧠 1
e
For the first link in the starting post - all is subjective - annotation is always above while
test("Name")
requires more to read test name. I feel I need to add more empty lines and move extensions to make things lighter. I will also check projects that use Test Balloon for ages to learn writing style there.
It is also new for me - maybe in time I will be just easy read them as junit one
I will try to formalise more feelings
o
As far as I understand it, in the above example the visual differences appear to be less in code size, but more in cognitive load on our 🧠 decoder:
Copy code
test("Get new resources - all entries are ordered by publish date description") {
is more efficiently processed than
Copy code
@Test
fun getNewsResources_allEntries_areOrderedByPublishDateDesc() = runTest {
Code size differences become more pronounced with parameterization. But for sure, with reasonably-sized efforts, we can only get close to actual perception. Ideally, we'd have a "treatment" group (TestBalloon), a control group (JUnit), measure time spent on comprehension, and then measure the actual level of comprehension. And yes, habits matter a lot, like on the web where we routinely skip ads appearing in the usual places.
e
Almost except I write test function names with ticks `fun `Get new resources will be ordered`()`
And, thank you for the discussion!
❤️ 1
o
Sure, you can do better with backticks, though it's always easier with unrestricted strings (allowing slash, dot, semicolon, ...).
b
> Ah, should be easy-peasy. I'm thinking about something like
val DB = testFixture{}
but should be
fun TestScope.DBFixture() = testFixture{}
Also keep in mind that "If a fixture's value is an
AutoClosable
, the fixture will use its
close()
method, without requiring
closeWith
." [docs] I sometimes find that my test fixtures are cleanest when defined as instances of
AutoCloseable
-implementing classes. The class implementation can be arbitrarily long and complex, but implemented separately from the tests that use it, just like you'd do with any other class that you wanted to reuse in multiple places. Instantiating and using a fixture is then as simple as:
Copy code
... by testSuite {
   testFixture { MyFixture() } ...
}
Note in particular the absence of `closeWith`: fixture shutdown code is hidden away in `MyFixture`'s
close()
method.
thank you color 1