What is TDD?

Objective/s:
  • To understand what TDD is
  • How different TDD is versus the traditional way of developing software.
  • Why it's important to write tests first
  • Learn the benefits of using TDD
  • Learn when to use and when not to use TDD

What is TDD?

TDD or Test Driven Development is the practice of writing tests before we write code. The idea is we would make assertions of what we would like our code to do before we actually write the implementation.

We are "testing" our application all the time: We write the code, test our app manually (via the browser or the console/terminal), make code revisions, repeat. This is how we currently develop our app. But with TDD, the order is different: 


We'll use tools like Selenium and Mocha to automate the testing for us.


Why Write The Tests First?

A lot of people see the benefit in writing tests before writing the code. Others would disagree. They think that it doesn't matter whether you write the tests before or after the code. But in TDD, it's really all about adopting a practice for ensuring that you're writing testable code.

TDD forces you to consider how you design your code. If you take an approach of writing tests after you've written a lot of code, there's a chance that you will have written some code throughout that process that's going to be really hard to extract and isolate just to be able to test what you've written.

How do you "test" an app?

As mentioned earlier, you are already used to testing your app the traditional way (via the browser or the terminal/console). But with TDD, we'll use certain tools that our app needs to install, import, and invoke. These tools are going to be invoked in specific test files within your project folder (typically stored in a /test folder) and you are going to write the test code. You will then need to run a test commandand see test results (in the terminal/console).

Before diving into the meaty part of the subject, you must already have this mindset: that at this point, before writing any line of code for that awesome app you are thinking of, you need to have the test code for it first.


You won't probably enjoy it at first. As developers, and especially during our training, we are usually motivated to building that app as soon as possible and you might probably be thinking that writing tests would just waste your time or just hinder our productivity. Well, that's kinda true... When you're just thinking about creating Hacker Hero assignment apps.

Yes, real-world products that gets delivered the fastest are still much preferable, but it must not compromise quality, and maintainability. And TDD just solves that. And in terms of speed, yes it might take you a while in the early stages of the app creation, but those tests you wrote basically just saved you from the hundreds of hours you will definitely be spending on debugging code in the later stages. So speed-wise, TDD still wins.

So.. You still think TDD is NAH?

Okay, so I mentioned "test code". That's what usually puts people off.

Do I really have to write a code... just to test.. MY CODE?! That just means instead of writing X lines of code, I'd be writing 2X as much!

Well the answer is, YES! I'm sorry. But don't worry! It might take you a while for you to get comfortable with the test tools, but the concept is really really easy. Here's the whole idea of how testing works.

So, you have this code for your app:

fairness.js

class Fairness {
    // a method that simply returns true if a & b is equal, false otherwise.
    is_fair(a, b) {
        let result = false;
        // do stuff here
        return result;
    }
}
module.exports = Fairness;

And below is just the basic test file, to test your code:

test.js

``` // some necessary import stuff here // const Fairness = require('./fairness'); describe("Fairness", function() { it("should return false if values passed are not equal", function(){ let fairness = new Fairness(); let result = fairness.is_fair(1, 2); expect(r...