Kotlin tests and user stories.

Mike Solomon
Aug 14 · 2 min read

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.

First sign into your console.
Then check your point balance. There should be fifteen points.
Then use all of your points to buy a ticket to Guam that costs exactly the number of points you have.
Then check your point balance. It should now be zero.

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


Random musings about the things we do to make the things we make do what they should do.

Mike Solomon

Written by

C.E.O. of meeshkan.com / Artistic director of ensemble101.fr / Meeshkan’s hiring! http://bit.ly/2MqvCiZ


Random musings about the things we do to make the things we make do what they should do.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade