My reasons for TEST-FIRST approach

Ishank Sharma
2 min readApr 2, 2018

TL;DR; START UNIT TESTING NOW

This is my story about how from now on I’ve decided most of the client code I ever write will be using test-first approach, but first for those who don’t know what unit testing is.
Unit(Single) testing implies that we test each unit of functionality in our code so that it performs its intended task.That unit may span multiple functions or classes but in unit testing, we tend to keep the functions 30 lines or less.
Let’s look at a quick example…

Just read test like normal English , they’re supposed to be like this

But Why?

Well if we just look at this example it’s not really sure why did we even bother with unit testing an that’s the issue with most testing tutorial, the reader in case is new to the testing paradigm is not sure why we even bother with testing in the first place.However if we look closer at this example there is some knowledge to be conferred.
Imagine we import a fancy function like this from a Javascript app ,we can just run the test without even running the app.
Using unit test the development flow somewhat changes ,

NEVER HAVE TO RUN THE PROGRAM TO KNOW IF ITS WORKING

Above reason is enough for someone who has to make long forms with arbitrary fields to switch to test first approach.I have seen code with 20 or more form fields and trust me it is really painful when we have to fill all the fields just because I changed a small API.

Though testing at a upper level such as functional and integration testing are most popular ,for anyone who writes tons of code and not use pre made well unit tested modules it has become imperative.

Imagine you have changed an API end point in your app , are you going to run all the possible dependents of that API again manually ?
You need unit tests.
Do you dread the possibility and uncertainty of changing a small function and introducing any number of bugs?
You need unit tests.
Do you think you can remember all the possible errors that could happen when someone touches your code and eventually break things?
You need unit tests.

Cons

  • You have to have a good understanding of what you’re building otherwise it’ll be harder to articulate the test.
  • Increases initial time investment, but pays off later.
  • Are those really that bad things to turn you away from testing?

Got hooked? Here’s a really good article to keep you interested.
https://medium.com/welldone-software/an-overview-of-javascript-testing-in-2018-f68950900bc3

That’s all folks ,Enjoy!

--

--