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

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
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.

About these ads

About Vikas Hazrati

Vikas is the CTO @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. We do niche product and project development on Scala and Java. We consult and coach on effective software development and agile practices. With our focus on software craftsmanship you can be assured of a good quality at the right price. To know more, send a mail to info@knoldus.com or visit www.knoldus.com
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s