What is Unit Testing?
A unit is called as a module/component/function in any programming language. Unit tests exist to test individual units of software functionality. Units of the software make sure that your code is written in the modular way, where it can achieve the cohesion and loosely coupled system.
If it’s hard to write unit tests for software without mocking lots of other things, that’s a sign that software is not modular enough. Every module should have unit tests, and every application should be made up of modules.
As each unit will function properly your whole software functionality will never fail. It also symbolises the most important principle of programming which is DRY(Do Not Repeat Yourself.) which in turn reduces the code size.
Benefits of Unit Testing
In terms of software development and deployment time and performance of the application are the most important factors. There are large amount of benefits can be earned by performing the Unit Testing, some of them are as follows:
- Accuracy: Unit testing makes sure the breakpoint of any collision/failure in the software.
- Speed: It increases the integration process by combining tested modules/units which in turn increases the speed of the software development.
- Cost: Huge amount of cost reduction in terms of the time as continious development doesn't affect by the testing time.
- Modularization: Part of the system can be used in other system with unit testing.
Let's start with unit testing in the nodejs. Nodejs is popular langugae now a days for its performance. As the development platform is runnning fast so we need to have unit testing libraries to test the applications. There are many libraries/module available for unit testing in the Nodejs. Some of popular examples are like mohca, jasmine, TAPE etc. Each one has its own pattern of writing test cases and pros and cons.
Let's start with the "TAPE" unit testing in nodejs.
TAPE
TAPE(Test anything protocol) is the one of the most popular "Unit Testing" module for nodejs platform. It comes with really simple syntax, easy configurations, No Globals concept, no external assert libraries, no shared states among the tests.
TAPE has some advantages as compare to other modules.
- Too much configuration: Choose an assertion library, chose a reporting library.
- Globals: Mocha provides the global variables like describe, it which you never declared or initalised.
- Shared State: Functions like `beforeEach` and `afterEach` actively encourage you to do something youk definitely should not do. This functions can be usually used to do test setup, teardown for individual
Getting started
Let's get started with the "Unit Testing" using TAPE.
Install
npm install tape --save-dev
First we need to install the TAPE under our nodejs application under dev-dependencies(as it will be required under development environment only). No need for global installation.
Start developing your test cases by requiring the tape in each file, in the end, you can require all the files in 1 single file and then execute the single file using TAPE.
Start developing your test cases by requiring the tape in each file, in the end, you can require all the files in 1 single file and then execute the single file using TAPE.
Reporting
TAPE also provides the result of "Unit Tests" in the human readable format on the console, to improve the reporting there are the large number of libraries available in the npm.To use them, try the following:
node test/index.js | tap-spec // type the module name from npm for reporting
you can also customize the reporting by creating your own sort of reporter. For more details refer.
Sample Example
var test = require('tape');test('timing test', function (t) {t.plan(2);t.equal(typeof Date.now, 'function');var start = Date.now();setTimeout(function () {t.equal(Date.now() - start, 100);}, 100);});$ node example/timing.jsTAP version 13# timing testok 1 should be equalnot ok 2 should be equal---operator: equalexpected: 100actual: 107...1..2# tests 2# pass 1# fail 1