How to Perform Test-Driven Development

Victor Yohanna
5 min readJan 30, 2021

In the last article we have seen what Test-driven development is and why use test-driven development. In this section we are going to highlight some advantages of Test-driven development and see practical example of Test-driven development.

Testing is an important part of any major project you are working on. It sounds pretty obvious when you add a new feature, open your browser and run the code to see how it looks like. This is called manual testing.

In manual testing, each time you add a new feature or you modify existing one, you need to test the entire features again and again.

Manual testing is also a good idea but automated testing is better and more simplified.

Advantages of Test-driven Development

Let’s explore some advantages of test-driven development when implemented in a project

1. Good program structure and code quality: TDD requires you write test first before writing the code, therefore, you need to define all functional requirements and the goals you want achieve. This helps you produce better structured and quality code.

2. Saves time and reduce cost: TDD makes it easy to identify and fix bugs which ensure adding new feature or modifying existing ones does not break the entire system. It reduced time and cost in a long run.

3. Project documentation: Detailed project documentation may likely include all users’ actions and so on. Implementing TDD makes you write test base on particular requirements and you are bound to create strict and detailed specifications.

4. Eliminate fear of change: TDD gives you the confidence to change the larger architecture of an application when adding new functionalities. Without the flexibility of TDD, frequently adding new functionality to the existing application can cause problems down the road.

Different Types of Testing

1. Unit Testing: This means testing individual modules of an application in isolation (without any interaction with dependencies) to confirm that the code is doing things right.

2. Integration Testing: This involves testing more than one modules integrated together. It means testing modules that depend on the results of another module.

3. Functional Testing: Also known as End-To-End (E2E) Testing. This involves testing an application workflow from beginning to end.

Unit testing considers checking a single component of the system whereas functionality testing considers checking the working of an application against the intended functionality described in the system requirement specification. On the other hand, integration testing considers checking integrated modules in the system.

Example of Test-driven Development

In this example we are going to see how to implement TDD in JavaScript but you can use any programming language of your choice.

I will be using Nodejs runtime environment and vs code as my text editor. You can use any text editor because the steps are similar.

The following are the steps involve.

1. Create a new project directory and give it a name

2. Lunch vs code >>> open folder and select your newly created folder

3. Open terminal: Vs code comes with an integrated terminal which starts at the root of your workspace. This can be convenient as you don’t have to switch windows to perform command-line task.

4. Create package.json: every node project must have a package.json file.

From the integrated terminal type npm init -y or yarn init -y depending on what you are using then press enter. I am using yarn

5. Install JavaScript test library:

Jest, QUnit, Mocha, and Jasmine are currently the most popular (it doesn’t matter that much which one you use, since they all essentially do the same thing). For this article I’ve arbitrarily chose Jest.

Install Jest

yarn add — dev jest

Or

npm install — save-dev jest

We are going to create a simple test that add two numbers

6. Create a file named sum.test.js in the root folder. This will contain our actual test:

Now add the following test to sum.test.js:

test(“adds 1 + 2 to equals 3”, () => {

expect(sum(1, 2)).toBe(3);

});

Before we run our test, add the following section to your package.json:

“scripts”: {

“test”: “jest”

}

7. Run test: To run the test, type yarn test or npm test on you terminal, depending whether you are using yarn or npm.

It is expected the test should fail because we have not yet written the actual code.

Below is our test result

8. Create index.js file in the root folder and add the following code

function sum(a, b) {

return a + b;

}

module.exports = sum;

9. Modify sum.test.js by calling actual function written in index.js. it will now look like this;

const sum = require(“./index”);

test(“adds 1 + 2 to equals 3”, () => {

expect(sum(1, 2)).toBe(3);

});

10. Run the test:

That’s it! You have successfully written your first test using Jest

--

--

Victor Yohanna
0 Followers

Software Developer interested in Data Science, web and mobile application development