Kotlin tests and user stories
10th Jul 2020
This article was originally published on August 14, 2019.
I love stories. I love tests. And I really love tests that read like stories.
My journey into the poetic world of writing tests like stories starts with a talk by Jake Wharton about robot testing. Wharton argues that tests should read like instructions to a robot on how to use your app. The benefits of his approach go beyond the human-readability of a testing spec. Writing hybrid story/tests enforces a form of rigor that forces one to take a step back and think about how one truly wants users to interact with an app. This invariably leads to a cleaner design and less cruft.
This way of authoring tests has recently become a bit of an obsession. So much so that I have dispensed with functional syntax and begun writing my tests using prose. Think OuLiPo plus Larry Wall.
“How does the whole thing work?” you may ask. Imagine we were writing a payment app. The code below would constitute a syntactically-valid Kotlin test.
Kotlin makes the example above possible thanks to the keyword. I have provided below the same example with all of the infix functions needed to create syntactically correct Kotlin.
You see that some functions above contain placeholder comments to verify the various bits of business logic that we are testing. This allows us to decouple the actual functions we are testing from their semantic meaning. It also reduces the ambiguity of intent even though it requires a fair amount of boilerplate. Even the boilerplate can be mitigated to a certain extent by scripts that transform prose into code. Robot tests are not the only situations where Kotlin-interpretable prose can prove useful. Medium articles work as well.
Don’t miss the next post!
Absolutely no spam. Unsubscribe anytime.