HDB. Powered by Blogger.

Formulir Kontak

Name

Email *

Message *

statistics

Recent

Comment

Subscribe

Categories 2

Find Us On Facebook

Instagram posts

Find us on Facebook

Flickr Images

Most Popular

About us

About

Facebook

Advertising

Advertising

Advertisement

Popular Posts

Monday, February 22, 2016

Nodejs Unit Testing with "Tape"

No comments :

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.

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.js
TAP version 13
# timing test
ok 1 should be equal
not ok 2 should be equal
  ---
    operator: equal
    expected: 100
    actual:   107
  ...
1..2
# tests 2
# pass  1
# fail  1

No comments :

Post a Comment