ATDD, ScalaTest and Scala

In my last post on ATDD, we had seen how it was easy with Cucumber to write a feature file and share it with our stakeholders in order to validate our understanding of the functionality. The idea again is to build the right software right. In this post, we would learn how to do ATDD with ScalaTest but before that let us do a recap of what we learnt

Acceptance Test Driven Development (ATDD) is a practice in which the whole team collaboratively discusses acceptance criteria, with examples, and then distills them into a set of concrete acceptance tests before development begins. If you notice the keywords here then they are

  1. whole team discussion
  2. acceptance criteria
  3. with examples
  4. concrete acceptance tests
  5. before development begins

The last post would give you enough information for getting started with Cucumber. However, if you are in a (most likely) situation where your stakeholders would rather validate the output of acceptance tests for you AND you would rather not add another framework to your existing stack then you might be interested in Acceptance Testing with our trusted ScalaTest. You can read more about ScalaTest here.

Again, if you recollect from the last post. We have a feature. Every feature has scenarios (which includes the happy path and alternate paths).
For all such scenarios we would specify the
1) Preconditions
2) Input
3) Output

The corresponding mapping in the feature file scenario would be
Precondition == Given, And
Input == When
Output==Then

Let us see how we would write something like this using ScalaTest. We would use exactly the same example that we used in the Cucumber post, which is

Now let us see how we write it using ScalaTest and FeatureSpec

As you would notice, we are doing exactly what we were doing with Cucumber with the exception that we are not writing a feature file first. We are actually writing the feature file as a part of the Test. The output of this test would be something which would be interesting to the stakeholders.

Let us run the test with

The output that we get is

ScalaTest output

Had the output gone bad, meaning that say we were expecting 11 instead of 12 then we would get the result like this
Selection_013

Thus looking at the output the stakeholders would be able to verify whether the functionality has been implemented correctly or not. The code for the current blog is present on the Knoldus Github account. In our next post we would look like some goodies like how to pass multiple values to a single scenario. Without that we would have to keep repeating scenario definitions which violates DRY (Don’t repeat yourself). 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

2 thoughts on “ATDD, ScalaTest and Scala

Leave a Reply

%d bloggers like this: