Wednesday, February 24, 2016

JASMINE - A Unit Testing Framework

Any small or medium or large App does require unit tests to verify quality of the code during development stage itself. And this provides flexibility to easily change or modify code without worrying about breaking any regressions.

Before even getting into let’s understand BDD S/W development process.  BDD as it stands for behavior-driven development emerged from Test-driven development. BDD comprises of techniques and principles of TDD to provide ease on collaboration.

Jasmine, a JS unit test BDD framework, for validating JS functions. Jasmine does not have any dependency on DOM elements nor depends on any other JS unit test framework. This comes with it’s own syntax to write efficient tests.

Here’s you can get code for Jasnmine:

Jasmine provides flexibility either to use Ruby or Python. Once the setup is done as per above given instructions, you can start developing unit tests. It is advisable to develop your unit tests first and then write code.

Angular framework has written by keeping testability in mind. Jasmine whole-heartedly support any app built using Angular just like Mocha or qunit etc

You want to write a unit test for a simple mathematics equation

Sample code:
Describe (‘Maths, function () {

//necessary
it(‘should add two numbers accurately’, function() {});
it(‘should subtracts two numbers accurately’, function() {});

});

describe() method describes test suite. It() describes spec.

For Angular’s simple controller you can write below unit test:
var myApp = angular.module('myApp',[]);  myApp.controller('GreetingController', ['$scope', function($scope) {   $scope.greeting = 'Hola!'; }]);

describe(‘GreetingsController Example’, function() {
var GreetingController,
scope;
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
.
.
.
.});

Now you have an example of basic unit test using Jasmine. You can run all your tests on every commit. These tests are very lightweight, and executes faster.
Achieving Continuous deployment makes it easy.

On Jenkins, you can install a command line with grunt plugin. Add the command line argument in your Jenkins config file.

You can see a sample output here:


No comments:

Post a Comment