Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Hello TDD!

Save for later
  • 6 min read
  • 14 Sep 2016

article-image

In this article by Gaurav Sood, the author of the book Scala Test-Driven Development, tells  basics of Test-Driven Development. We will explore:

  • What is Test-Driven Development?
  • What is the need for Test-Driven Development?
  • Brief introduction to Scala and SBT

(For more resources related to this topic, see here.)

What is Test-Driven Development?

Test-Driven Development or TDD(as it is referred to commonly)is the practice of writing your tests before writing any application code.

This consists of the following iterative steps:

hello-tdd-img-0

This process is also referred to asRed-Green-Refactor-Repeat.

TDD became more prevalent with the use of agile software development process, though it can be used as easily with any of the Agile's predecessors like Waterfall.

Though TDD is not specifically mentioned in agile manifesto (http://agilemanifesto.org), it has become a standard methodology used with agile. Saying this, you can still use agile without using TDD.

Why TDD?

The need for TDD arises from the fact that there can be constant changes to the application code. This becomes more of a problem when we are using agile development process, as it is inherently an iterative development process.

Here are some of the advantages, which underpin the need for TDD:

  • Code quality: Tests on itself make the programmer more confident of their code. Programmers can be sure of syntactic and semantic correctness of their code.
  • Evolving architecture: A purely test-driven application code gives way to an evolving architecture. This means that we do not have to pre define our architectural boundaries and the design patterns. As the application grows so does the architecture. This results in an application that is flexible towards future changes.
  • Avoids over engineering: Tests that are written before the application code define and document the boundaries. These tests also document the requirements and application code. Agile purists normally regard comments inside the code as a smell. According to them your tests should document your code. Since all the boundaries are predefined in the tests, it is hard to write application code, which breaches these boundaries. This however assumes that TDD is following religiously.
  • Paradigm shift: When I had started with TDD, I noticed that the first question I asked myself after looking at the problem was; "How can I solve it?" This however is counterproductive. TDD forces the programmer to think about the testability of the solution before its implementation. To understand how to test a problem would mean a better understanding of the problem and its edge cases. This in turn can result into refinement of the requirements or discovery or some new requirements. Now it had become impossible for me not to think about testability of the problem before the solution. Now the first question I ask myself is; "How can I test it?".
  • Maintainable code: I have always found it easier to work on an application that has historically been test-driven rather than on one that is not. Why? Only because when I make change to the existing code, the existing tests make sure that I do not break any existing functionality. This results in highly maintainable code, where many programmers can collaborate simultaneously.

Brief introduction to Scala and SBT

Let us look at Scala and SBT briefly. It is assumed that the reader is familiar with Scala and therefore will not go into the depth of it.

What is Scala

Scala is a general-purpose programming language. Scala is an acronym for Scalable Language. This reflects the vision of its creators of making Scala a language that grows with the programmer's experience of it. The fact that Scala and Java objects can be freely mixed, makes transition from Java to Scala quite easy.

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime

Scala is also a full-blown functional language. Unlike Haskell, which is a pure functional language, Scala allows interoperability with Java and support for objectoriented programming. Scala also allows use of both pure and impure functions. Impure functions have side affect like mutation, I/O and exceptions. Purist approach to Scala programming encourages use of pure functions only.

Scala is a type-safe JVM language that incorporates both object oriented and functional programming into an extremely concise, logical, and extraordinarily powerful language.

Why Scala?

Here are some advantages of using Scala:

  • Functional solution to problem is always better: This is my personal view and open for contention. Elimination of mutation from application code allows application to be run in parallelacross hosts and cores without any deadlocks.
  • Better concurrency model: Scala has an actor model that is better than Java's model of locks on thread.
  • Concise code:Scala code is more concise than itsmore verbose cousin Java.
  • Type safety/ static typing: Scala does type checking at compile time.
  • Pattern matching: Case statements in Scala are superpowerful.
  • Inheritance:Mixin traits are great and they definitely reduce code repetition.

There are other features of Scala like closure and monads, which will need more understanding of functional language concepts to learn.

Scala Build Tool

Scala Build Tool (SBT) is a build tool that allows compiling, running, testing, packaging, and deployment of your code. SBT is mostly used with Scala projects, but it can as easily be used for projects in other languages. Here, we will be using SBT as a build tool for managing our project and running our tests.

SBT is written in Scala and can use many of the features of Scala language. Build definitions for SBT are also written in Scala. These definitions are both flexible and powerful. SBT also allows use of plugins and dependency management. If you have used a build tool like Maven or Gradlein any of your previous incarnations, you will find SBT a breeze.

Why SBT?

  • Better dependency management
    • Ivy based dependency management
    • Only-update-on-request model
  • Can launch REPL in project context
  • Continuous command execution
  • Scala language support for creating tasks

Resources for learning Scala

Here are few of the resources for learning Scala:

  • http://www.scala-lang.org/
  • https://www.coursera.org/course/progfun
  • https://www.manning.com/books/functional-programming-in-scala
  • http://www.tutorialspoint.com/scala/index.htm

Resources for SBT

Here are few of the resources for learning SBT:

  • http://www.scala-sbt.org/
  • https://twitter.github.io/scala_school/sbt.html

Summary

In this article we learned what is TDD and why to use it. We also learned about Scala and SBT. 

Resources for Article:


Further resources on this subject: