Unit Test

@tuanquynet

Sep 29, 2014

Content

  • What is Unit Test
  • What is TDD
  • What is BDD
  • Testing frameworks
  • Demo
  • Wrapping Up

What is Unit Test

In computer programming, unit testing is a software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use.

How to perform Unit Test

What is TDD

  • Stand for Test-Driven Development
  • Different from traditional testing approach:
    Write unit code and write test for individual unit code before integration

First test Development

What is BDD

  • BDD stand for behavior driven development
  • Similar to TDD, it is First Test approach
  • Unlike TDD, BDD focus on user's perspective

Mocha TDD

Mocha BDD

var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });
});
var should = require('should')
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      [1,2,3].indexOf(4).should.equal(-1);
    });
  });
});

Testing Frameworks

Assertion Library

  • chai.js: TDD and BDD
  • should.js: BDD
  • expect.js: BDD
  • jasmine: BDD
  • mocha: TDD & BDD

Test Tool

Test Tool (Utility)

Testing Framework

Assertion Library

Test Tool Set

Basic Flow

Basic Flow

Demo

Advantages of Unit Test

  • Automate regression test in order to ensure the new code added don't break the other
  • Reduce the time for retesting whole system
  • Enforce the code style consistent

Disadvantages of Unit Test

  • Take time to write unit test code
  • Require skill to writing unit test
  • Running unit test takes time
  • Fixing defect takes time

Best Practises

  • Good unit test require clear user requirement
  • Should be atomic
  • Run fast
  • Run automatically when code check in
  • Easy to understand
  • Don't hit database unless necessary

Q&A

Unit Test

By Quy Tran