by

Writing test cases with given, when and then

At my previous workplace we worked with Groovy and Grails and I had the opportunity to use testing framework for Groovy called Spock.

Spock uses labels to divide test cases into different blocks. That way, data definition and final asserts are separated from the code under test, which is typically a function or a method call. In the article I want to show how this looks in practice and discuss adopting this style in other languages.

Let’s dive in! The basic block of code is called expect:

The advantage of this block in Spock is that everything in that block is assumed to be an assert. Without any method invocation or a special assert keyword we get a clear section to test our assumptions.

Combining expect with where block enables us to create parametrized tests. Spock would take all our data separately and run our test multiple times:

For tests that don’t need to be parametrized there is a block called setup. This block doesn’t do anything special and it just provides a visual separation for test requirements:

Spock also provides blocks for more BDD style test cases called begin, when and then. given is just an alias for setup, then behaves like expect and when gives us a block where we can put code under test:

I really like writing test cases using these blocks because it makes the tests more readable and so I decided to write my tests in other languages in a similar style. The test frameworks that I use unfortunately don’t have anything similar available, that’s why I decided to only use simple comments for now. It helps me to visually separate what is being tested, what are the test requirements and what are the asserts.

I opted just for using given, when and then blocks for simplicity and also because without additional library support, we’d not get any other benefits anyway. Let’s compare examples in Python written with and without these code blocks:

I am sure that in this simple example many people would prefer the shorter version of the test. What we can see however is that our second version of the test reads more like a natural text because of the additional words.

More often than not, we will end up with bigger test cases that will have a couple of lines for each block:

In this example we can see that the difference is noticable.

Even if this technique is just visual and doesn’t necessary carry any meaning in terms of code, I enjoy it and I find myself using it these days.

I looked whether there is a Spock-like testing framework or library available in the world of Python or JavaScript and the only one seems to be nimoy for Python. I haven’t personally tried it since I am using Pytest, but it seems that I am not the only one who enjoyed working with Spock.

So what do you think? Do you have any favourite DSL or a way to write tests? Let me know in the comments.

Also, if you are into testing and discussing best practices, maybe you will enjoy my other article Putting Test Case Specification directly in code.

Happy testing!

 

 

 

 

 

Loading Likes...

Write a Comment

Comment