What is TDD?

Lou Bison
4 min readMar 17, 2019

--

Photo by Louis Reed on Unsplash

Are you a software developer? If so, then testing is not something new to you. Testing ensures that the functions and methods we have written actually do what we intended. Automated tests also ensure that when we add functionality to our application, it does not break already existing functionality.

I am conversant with testing, but TDD?
The difference is in the priority. Usually, we write functional code first and then write tests for it. In TDD (Test driven development) tests are the priority. The tests are written first, and then the functionality. But how would the tests pass if, the functionality does not exist?

Well, am glad you asked. Actually, with TDD, the tests have to fail first, then the minimum required functionality is implemented to make them pass. That is TDD.

Why would I use TDD?
The beauty with TDD is that it narrows the focus of the developer. The developer's goal is to make the tests pass. The tests remember are testing the final output of the application. So by focussing on passing the tests the developer is intentionally or inadvertently achieving the goal of the application.

How do I do TDD?
Let me show you practically using an API I am collaborating on currently. For more about the app check out the repository over here.

I am working on the login feature. So let's do TDD by writing the tests first.

Login tests

We expect the tests to fail, and they do so spectacularly.

Let's proceed with TDD by writing the login functionality.

Now we run the tests again. And they pass.

Fantastic, we have done TDD.

Just a note on testing your product. When writing tests, focus on the input and output of your app. Test correct user input as well as bad user input. Ensure that your app handles all input cases gracefully without crashing.

To take our testing a step further, we want to let others know that our app is well tested and is actually working. For that, we will need a few tools. Two to be precise.

  • Travis
    Travis is a continuous integration tool that assures us that our app is still functional as we keep adding functionality.
Login Test passing

With Travis setup, every time we push to GitHub, Travis will automatically start a build, run our tests, and will send an email if the build is broken. If the tests are successful on the other hand, we get sparkling green “passing badge”.

  • Coveralls
    Coveralls is an online service that works with Travis to measure how much code our tests cover.

As you can see we have a 99% coverage. That is pretty good for our confidence in our apps tests.

Now we simply have to get the Travis and coverage badges, add pin them to our repository’s readme. Now whoever wants to collaborate on our app ought to ensure the app continues to build and test coverage should not go lower than our set percentage threshold.

With your app hooked to Travis and coveralls, with your builds passing and coverage above your desired threshold, you can rest assured your app is ready for deployment at a moments notice.

Conclusion
For fast and efficient app development, TDD is as a choice is a no brainer. Not only is it fun to pass your tests one by one, but your focus is also narrowed to achieving the major objectives of your application.

With this blog I am confident the next time you embark on a project, tests will drive your development.

--

--