DEV Community


Posted on


My experience with TDD and how to implement it in a project


TDD (Test-Driven Development) is a development technique where a developer writes a certain number of tests to check or test the output of functional code he/she has written.

For example I might write a function to multiply two numbers. In TDD, I must write a number of tests first with the desired outputs based on all use cases and then write the function that must pass all the tests.

In traditional development a developer writes a function, then tests the output with desired inputs. Which is the complete opposite of TDD.

If, like me, you are used to the traditional way of development, it might take some learning to get used to TDD.

However there are some available tools/frameworks like :
-Mocha, Jasmine for JavaScript and
-Pytest or UnitTest for python

These tools help in the test automation process.

My personal favorite for JavaScript is Mocha. Mocha is open-source which is great.

Let's go through the steps to install Mocha in your project.

-Nodejs v8.0.0 or newer

In your terminal, change the directory to your project's directory and type the following:

If you want to install mocha with npm globally:

 $ npm install --global mocha 

or if you want it as a development dependency for your project:

 $ npm install --save-dev mocha 

Now type the following lines to install mocha and create a new folder called 'test' and open a file named 'test.js' where we will write our test.

$ npm install mocha
$ mkdir test
$ $EDITOR test/test.js # or open with your favorite editor

In the test.js is where you will write your test.

Now, in your package.json file add this line under scripts

"test": "mocha"

Finally in your terminal type the following to test your function.

npm run test

As always, documentation always helps.

You may use other testing frameworks but for mocha, documentation can be found here:

Top comments (0)

50 CLI Tools You Can't Live Without

>> Check out this classic DEV post <<