The magic formula to improve your test suite

Agustín Serena
Unagi
Published in
3 min readJun 4, 2021
Illustration by Ivan Haidutski from Ouch!

There is no such thing as quality software without automated tests. Our software must be properly tested, so it’s important how we structured them. Therefore, in this article, I will give you some tips to write your tests in a readable and reusable way. After reading it, you will realize that without much effort we can improve them considerably.

Disclaimer: This article is actually not magic at all. My tips are based on the BDD (Behavior Driven Development) strategy and the use of the Gherkin language (Given-When-Then), although we’ll only take certain characteristics of each one. But please, don’t tell anyone 🤫

Let’s go there!

The idea is to modularize our tests by extracting the different actions into different methods in such a way that we have clear and understandable tests at a glance. This way, they can be read as if they were user stories or acceptance criteria.

Let’s see a basic example where we’ll test a list of products with and without filters. In one scenario we’ll verify the display of the list and, in a second scenario, we’ll add some tests to filter the products by name.

Note: for the examples, I’m going to use Rspec but feel free to use Minitest or any other framework. It really doesn’t matter.

Although these two scenarios verify relatively simple and concrete functionalities, we see that the tests have a significant length, and are not clear nor readable.

Now let’s go straight into the magic 😉 and re-implement the same tests using the concepts mentioned above. Here we extract the actions to private methods and define them using the Gherkin language:

Yes, I confess, the final code is longer 🤥 But nobody can deny that it’s more readable and clear. I bet that even someone knowing nothing about Ruby could get a general idea of what’s happening there. But you know what I like the most? We haven’t done anything new. Just by applying basic programming concepts, we really improve our test suite.

This second example is much clearer and has several advantages:

  • we can read the tests in a natural language, instantly understanding what each scenario verifies
  • we get rid of duplicated code by using private methods
  • no scenario contains data creation
  • facilitates test maintenance
  • makes code review easier

Having tests in our developments is as important as implementing them in a good, clear, and elegant way. These issues are often not given due attention, and we end up having tests that are difficult to understand and maintain.

At Unagi, we know that the quality of the tests defines the quality of the final product, which is why we have been using this type of technique for a long time. We know there is still more to improve, but this is a simple trick to do it.

And what about you? Do you use any other trick to improve your tests? We would love to read about it. Please, leave your comments and don’t forget to leave 👏🏻👏🏻👏🏻 if you liked this article.

Unagi is a software factory that provides design and development services for startups, small and mid-size companies. Read more about us on our website, blog, Twitter, or LinkedIn.

--

--