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

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

Feature: Multiplication 
  In order to avoid making mistakes
  As an accountant
  I want to multiply numbers

  Scenario: Multiply two variables
    Given a variable x with value 3
    And a variable y with value 4
    When I multiply x * y
    Then I get 12

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

package test.scala

import org.scalatest.FeatureSpec
import org.scalatest.GivenWhenThen
import scala.collection.mutable.Stack
import org.scalatest.prop.TableDrivenPropertyChecks

class MultiplicationSpec extends FeatureSpec with GivenWhenThen {

  feature("Multiplication") {

    info(" In order to avoid making mistakes")
    info("As an accountant")
    info(" I want to multiply numbers")

    scenario("Multiply two variables") {

      given("a variable x with value 3")
      val x = 3
      and("a variable y with value 4")
      val y = 4

      when("i multiply x * y")
      val result = x * y

      then("i get 12")
      assert(result === 12)



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

sbt test

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

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.

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. 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). To know more, send a mail to or visit
This entry was posted in Scala and tagged , , , . Bookmark the permalink.

2 Responses to ATDD, ScalaTest and Scala

  1. Pingback: ATDD, ScalaTest, Cucumber and respecting DRY | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s