ATDD, ScalaTest, Cucumber and respecting DRY

In our last post we looked at how it was easy to use ScalaTest for doing acceptance testing. In a post prior to that, we had looked at doing the same with Cucumber. We had also concluded that though acceptance tests could be written in the way described however, there was an issue. Writing the feature files with individual features having multiple scenarios which would just be a repetition with change of values does not seem smart. It violates DRY (Don’t Repeat Yourself)

For instance, it would be clunky to have something like this

Ok, it can be done but is wasteful. Cucumber gives us a way to pass multiple values to a scenario. Look at the example below

If you notice, what we are doing here, you would realize that we are not changing our mapping code. You can read the mapping code here. We are simply creating a feature file which has like <x_1> , <y_1> etc. Then, we have an Examples: table which gives the values of the variables. The mapping code picks up values and results from here. You would find the complete Scala code for this on the Knoldus Github account.
Ok, so it is easy with Cucumber.

Let us see how we would achieve the same with our trusted ScalaTest. Look at the code below

The interesting things to note in the code snippet above are the following. We have,

1) Mixed in another trait called TableDrivenPropertyChecks
2) We created a table with values

3) We then did an iteration over the table and used all the values therein

4) We asserted for the actualResult and the expectedResult
5) And of course, we presented the following result to the stakeholders

Selection_019

6) If you noticed that ScalaTest ignores the first Tuple which is the table header ==> (“Number1”, “Number2”, “Result”). Isn’t that cool 🙂

That is all to it. The entire code can again be found on the Knoldus Github account.

Thus, as you would notice, in both the scenarios i.e. Cucumber and ScalaTest it is easy to pass in multiple values to your scenarios. In the next post, we would take a quick look at generating test data for passing to our scenarios. Till then.

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

1 thought on “ATDD, ScalaTest, Cucumber and respecting DRY

Leave a Reply

%d bloggers like this: