Test Driven Development

Learn to code, learn web development basics, start a career in tech.

Test Driven Development

Last edited: Jul 12 2019

Test Driven Development

Test-driven development (TDD) is a software development process that relies on driving the development process by first writing code that expects a certain outcome, then the software code is written to make sure that outcome is met.

Simple Theory of TDD

Imagine you want to create a login page, but before you do that, you first write a code that visits a url called "/login.html" and that code expects to find a username field and password field and a login button.

Test Driven

When you run that code, it will fail, because there is no login.html and therefore no username and password fields or login button, but now we have some expected outcome, time for us to write some code to make it pass, what we have done is write the test first.

Implement Solution

We can go ahead and create the login.html file, add the username and password fields and login button, now when we run our test code again, it will pass, yay! this process of writing a test to fail first and then implementing a solution for it to pass is what we mean by Test Driven Development (TDD)

Benefits of Test Driven Development

Let's explore a list of benefits for TDD and generally having well-tested codebase in general.

1. Better Focus and Planning

A significant benefit of TDD is that it encourages forward-thinking, essentially it forces you to plan what you expect of your program before even writing the actual code, this shift in thinking might appear to be a small one but in a long run it fixes a lot of time waste and speeds up learning.

2. Acts as Documentation

With test cases written it means that it is very easy for anyone to see a list of all the features that your program has, and not only that, but also how to test each functionality, what variables are required, in what order and format, your test cases will act as a living documentation with examples.

3. Easier to Maintain Codebase

Imagine trying to refactor a piece of code and not knowing what other parts of the software rely on that piece of code, with a Test backed project, you can make the changes and then run the test suite to make sure that all test cases pass, including the new one you added, this makes refactoring less scary.

4. Fewer Bugs

One of the common ways that bugs creep in is when we refactor a piece of code and introduce a side effect that is not immediately obvious, this can remain hidden until a user of the software tries to perform a particular action only to encounter an error, having a tested codebase helps prevent such side effects.

5. Increased Value

If no tests result in more buggy codebase, then a companies employees will spend more time fixing bugs and less time actually adding features of value, so increasing test coverage will result in less time spent fixing bugs which in turn increases value for both companies and developers.

6. Easy Upgrades

If you have written your code with a specific version of a programming language or framework, without test suite, how could you upgrade with the confidence that your program still works as expected? without test suite this is not possible, you would have to manually test every feature, and this is not possible without incurring huge costs, and in most cases, this is actually impossible and best to start from scratch.

Example Test Frameworks

Below are some test frameworks used in various programming languages.

RSpec - RSpec is a 'Domain Specific Language' (DSL) testing tool written in Ruby.

Bacon - Bacon is a small RSpec clone providing all essential features

JSUnit - JsUnit is a Unit Testing framework for client-side (in-browser) JavaScript.

ScalaTest - A testing tool for Scala and Java developers.

JUnit - JUnit is a simple framework to write repeatable tests.

pytest - The pytest framework makes it easy to write small tests.

PHPUnit - PHPUnit is a programmer-oriented testing framework for PHP.

Atata - C#/.NET automated web testing full featured framework.

Spock - Spock is a testing and specification framework for Java and Groovy applications.

EUnit - EUnit uses many preprocessor macros to be nonintrusive.

DUnit - DUnit: An Xtreme testing framework for Borland Delphi programs.

CurlUnit - CurlUnit is a simple framework to write tests in the Curl language

CppUnit - CppUnit is a unit testing framework module for the C++ programming language.

ASPUnit - ASPUnit is a unit testing framework based on the architecture of JUnit.

shUnit2 - shUnit2. shUnit2 is a xUnit unit test framework for Bourne based shell scripts

Later we shall explore the Rspec test framework for writting Unit Tests and Behaviour Driven Tests BDD

In Conclusion

The purpose of this article was to cover the topic of TDD, the process of writing failing tests first and then writing an implementation that will fix those failing tests. I also highlighted some benefits of using the TDD approach and why I think everyone should use this approach.

I also want to point out that, having tests is more important than TDD, if you struggle at first to write failing tests, I encourage you to cover your codebase with tests, even if you wrote the code first then reverse engineer a test for it, this at least will help prevent future bugs creeping in.