Software Testing In A Nutshell
Tags: watercooler, slimcoder
Published: 2021-02-13 20:45:05

---
title: Software Testing In A Nutshell
published: true
description: 
tags: watercooler, slimcoder 
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/l50stvoj8ds2szv9nfvo.jpeg
---

Software Testing is a method to check whether the actual software product matches expected requirements and to ensure that the software product is defect-free. This article only focuses on the unit, integration, and API but there are more than 150 types of testing types and still adding. Also, note that not all testing types are applicable to all projects but depend on the nature & scope of the project.

## Importance
When a software developer, developed a new feature or make changes to an old one, It’s compulsory to write a test for that change or feature so that the PR (Pull Request) Reviewer or QA (Quality Assurance) Engineer should know what your code is doing by looking into your test.

Software Testing can also help in process of CI/CD and pre-commits hooks if you are maintaining your code using version-controlling platforms i.e GitHub, GitLab, etc. If you decided to write tests from the very beginning of your project, This will also help you in covering edge case when your project codebase increase then tests will help you to know which case was failing because of a new feature or change.

## Prerequisite
Before going deeper into Software Testing, You should know that I’m using JavaScript frameworks and libraries to test my code.
In this article we will cover 3 types of Software Testing namely:
1) Unit Testing
2) Integration Testing
3) API Testing

Here is the list of tools and software's which I'm using for Software Testing namely:
1) Node.js
2) npm
4) Jest
5) Supertest
6) Expess.js

## Let’s Start
First, install Node.js and NPM for your operating system so that you can run JavaScript code, download and install packages from Node Package Manager.

I’m using Visual Studio Code IDE (Integrated Development Environment) but you can use any IDE for yourself.

## 1) Unit Testing
It is the process of testing a single software unit. For example, your team created a new feature that is responsible for adding two integers using JavaScript.

{% gist https://gist.github.com/slimcoders/7c04f3a48cdf8477c330f28cb9ffdfde.js %}

Add this file within your project directory and then run node add.js. You can see that it prints 3 to the console.

![Addition](https://dev-to-uploads.s3.amazonaws.com/i/mhs8lcyv5kg8yaku3jdw.gif)

I know now you may be wondering how to test this unit right? Relax! I will show you.
You successfully developed a feature that is responsible for adding two numbers and now can simply test it by installing jest cli from npm.

Install jest-cli globally by using this command: npm install -g jest-cli

This command will install jest globally within the system, after installing jest-cli you can use the jest command directly from the terminal.

But still, you need to configure jest so that you can test the add.js file; create a new file jest.config.json and add this within the project directory.

{% gist https://gist.github.com/slimcoders/596205fbe3d4f7e114a8facedf873fa9.js %}

After creating jest.config.json file within the project directory, now you will be able to test your add.js file; So to test this code create a new file as add.test.js.
All test files should have .test before .js extension so that jest should know that.

{% gist https://gist.github.com/slimcoders/fc884f7872463fd458419ac5c6594a94.js %}

As you can see below this is my add.test.js which is testing sum() within the add.js file.

In this file, I first import sum() from add.js file then created a describe() that is responsible for describing my test, and within its callback, I created it() which is responsible for testing my sum(). Remember that your it() and describe() should have a description so that everyone should know what this unit is doing.
Now let's run this test by using this command:

jest

![Addition Testing](https://dev-to-uploads.s3.amazonaws.com/i/mlgrftapieby9drwbb2j.gif)

As you can see above we successfully test our add.js using jest and our test is passed.

## 2) Integration Testing
It is the process of testing combined units. For Example, Your team now decided to perform arithmetic operations in a way that it should be able to add and subtract those two numbers. So here we go with the new file arithmetic.js according to requirements.
But before directly jumping to arithmetic.js we need to create a new unit sub.js which will be responsible for subtracting two numbers.

{% gist https://gist.github.com/slimcoders/1c8a28d64514feeee97e5ae4f8c72ce7.js %}

Now we will create a new file arithmetic.js which will be responsible for the addition and subtraction of two numbers by combining add.js and sub.js into arithmetic.js.

{% gist https://gist.github.com/slimcoders/8f32109c7c9dafb6ad869754c1540b49.js %}

Add this file within your project directory and then run node arithmetic.js. You can see that it prints “3:1" to the console by adding and subtracting 2 with 1.

![Integration](https://dev-to-uploads.s3.amazonaws.com/i/j2auhpvz90ghv6fggltf.gif)

It is good practice to write a unit of sub.js, which we previously created so here it is;

{% gist https://gist.github.com/slimcoders/84f3f123efb4a4a2034eee98e782654b.js %}

Now just run sub.test.js using this command: jest -- sub.test.js

This command will only run a single test which is sub.test.js got it?

![Substraction Test](https://dev-to-uploads.s3.amazonaws.com/i/3r09hma3fr4767wsfikw.gif)

After testing both units add.js and sub.js, This is the right time to test arithmetic.js so here is the integration test of it.

You can run this testing using this command: jest -- arithmetic.test.js

{% gist https://gist.github.com/slimcoders/98d54886f985fe399374f23327fc5aa6.js %}

## 3) API Testing
API (Application Programming Interface) testing is a type of software testing that aims to determine whether the APIs that are developed meet expectations when it comes to functionality, performance, reliability, and security for an application.

So now we will create an API using the De-Facto framework of Node.js known as Express.js.

First initialize npm within the project directory, so that we can install modules: Express.js, and super test for our use case.
Initialize npm within project directory this command;
npm init

Here is package.json from npm init and after installing modules.

Installs Express.js, Body Parser, and Supertest by this command;
npm install express supertest --save

{% gist https://gist.github.com/slimcoders/d1e8e64b84d771bfca0a5165959ce366.js %}

After installing these libraries and frameworks you can see node_modules folders created by npm and added as package.json dependencies.

Now create a new file server.js which we will responsible for running Express.js server and using arithmetic.js as an API.

Here is the codebase of running the server and using arithmetic as API.

{% gist https://gist.github.com/slimcoders/bc39d3634570bf9bee9e399beb49380a.js %}

Run server.js using this command:
node server.js

![Server](https://dev-to-uploads.s3.amazonaws.com/i/yfgjjhgbuwnhgydmctcc.gif)

After the server is successfully running you can now see the output of arthematic.js file on localhost:3000.

![localhost:3000](https://dev-to-uploads.s3.amazonaws.com/i/arktgwisntfplrovc1c5.gif)

Now we set up the server using Express.js and we verify that it is running and sending responses on localhost:3000.

Now we are on right track to writing an API test of API which we created so here it is;

{% gist https://gist.github.com/slimcoders/dff05a318ef50ef4827b7b80cac20cd5.js %}

Now our test is ready we added two assertions one for status from API and the second for response texted;

Run API test using this command: jest -- server.test.js

![API Testing](https://dev-to-uploads.s3.amazonaws.com/i/2enxgo212lm1zhbydcwx.gif)

## Conclusion
This article was a complete package of testing your code and API using jest and super test. Testing types which we covered were unit, integration, and API.

In this article, we learn that how software testing is important for software developers and his team. We also look into practical ways to test code and API but if you are interested to learn more then don’t forget to look into automation testing also, I will cover it in my next article.

Thank you for reading this article.