SlideShare une entreprise Scribd logo
1  sur  32
By : Akanksha Arora
Points to Discuss 
 Unit Testing & Test Driven Development 
 QUnit 
 QUnit API 
 Automated Testing (a brief introduction)
Unit Testing 
 In computer programming, unit testing is a procedure 
used to validate that individual modules or units of 
source code are working properly. 
 Unit testing is used for 
(i) Test Driven Development 
(ii) Fixing bugs 
(iii) Regression testing
Test Driven Development 
 Test-Driven Development (TDD) is a computer 
programming technique that involves repeatedly first 
writing a test case and then implementing only the code 
necessary to pass the test. 
 Test-driven development is a method of designing 
software, not merely a method of testing.
The TDD Micro-Cycle
Fixing bugs/Regression Testing 
 Fixing bugs 
 Regression testing
What do you need? 
 A Unit Testing framework 
 Development Environment
QUnit 
 QUnit is a powerful, easy-to-use JavaScript unit testing 
framework. 
 It's used by the jQuery, jQuery UI and jQuery Mobile 
projects and is capable of testing any generic JavaScript 
code, including itself! 
 Supports the same browsers as jQuery 1.x. That's IE6+ 
and Current - 1 for Chrome, Firefox, Safari and Opera.
Getting Started 
 Just qunit.js, qunit.css, and a little bit of HTML 
 A Quick Demo: 
http://jsfiddle.net/akankshaaro/rq31v84h/
QUnit API 
 Test 
 Assert 
 Async Control 
 Callback
Test : QUnit.test( name, test ) 
 Adds a test to run. 
 Testing the most common, synchronous code 
Qunit.test(“name of the test”, function() { 
//write down the assertions 
}); 
function: Function to close over assertions
Test : expect() 
Specify how many assertions are expected to run within a 
test. 
If the number of assertions run does not match the 
expected count, the test will fail. 
test(”expected assertions", function() { 
expect( 2 ); 
//two assertions are expected 
});
Asserts 
 ok 
 Equal 
 notEqual 
 strictEqual 
 notStrictEqual 
 deepEqual 
 notDeepEqual 
 throws
Assert - ok() 
ok( state, message ) 
A boolean check, passes if the first argument is truthy. 
test("ok”, function() { 
expect(3); 
ok(true, "passes because true is true"); 
ok(1, "passes because 1 is truthy"); 
ok("", "fails because empty string is not truthy"); 
});
Assert - equal() 
equal( actual, expected, message ) 
A comparison assertion that passes if actual == expected. 
test("equal”, function() { 
expect(3); 
var actual = 5 - 4; 
equal(actual, 1, "passes because 1 == 1"); 
equal(actual, true, "passes because 1 == true"); 
equal(actual, false, "fails because 1 != false"); 
});
Assert - notEqual() 
notEqual( actual, expected, message ) 
A comparison assertion that passes if actual != expected. 
test("notEqual”, function() { 
expect(3); 
var actual = 5 - 4; 
notEqual(actual, 0, "passes because 1 != 0"); 
notEqual(actual, false, "passes because 1 != false"); 
notEqual(actual, true, "fails because 1 == true"); 
});
Assert - strictEqual() 
strictEqual( actual, expected, message ) 
A comparison assertion that passes if actual === expected. 
test("notEqual”, function() { 
expect(3); 
var actual = 5 - 4; 
strictEqual(actual, 1, "passes because 1 === 1"); 
strictEqual(actual, true, "fails because 1 !== true"); 
strictEqual(actual, false, "fails because 1 !== false"); 
});
Assert - notStrictEqual() 
notStrictEqual( actual, expected, message ) 
A comparison assertion that passes if actual !== expected. 
test("notStrictEqual”, function() { 
expect(3); 
var actual = 5 - 4; 
notStrictEqual(actual, 1, "fails because 1 === 1"); 
notStrictEqual(actual, true, "passes because 1 !== true"); 
notStrictEqual(actual, false, "passes because 1 !== false"); 
});
Assert - deepEqual () 
deepEqual( actual, expected, message ) 
Recursive comparison assertion, working on primitives, 
arrays and objects, using ===. 
test("deepEqual”, function() { 
expect(3); 
var actual = {a: 1}; 
equal( actual, {a: 1}, "fails because objects are different"); 
deepEqual(actual, {a: 1}, "passes because objects are equivalent"); 
deepEqual(actual, {a: "1"}, "fails because '1' !== 1"); 
});
Assert - notDeepEqual() 
notDeepEqual( actual, expected, message ) 
Recursive comparison assertion. The result of deepEqual, 
inverted. 
test("notDeepEqual”, function() { 
expect(3); 
var actual = {a: 1}; 
notEqual( actual, {a: 1}, "passes because objects are different"); 
notDeepEqual(actual, {a: 1}, "fails because objects are equivalent"); 
notDeepEqual(actual, {a: "1"}, "passes because '1' !== 1"); 
});
Assert - throws() 
Assertion to test if a callback throws an exception when 
run and optionally compare the thrown error. 
test("throws”, function() { 
expect(3); 
throws( 
function() { throw new Error("Look me, I'm an error!"); }, 
"passes because an error is thrown inside the callback” 
); 
throws( 
function() { x // ReferenceError: x is not defined }, 
"passes because an error is thrown inside the callback” 
); 
throws ( 
function() { var a = 1; }, 
"fails because no error is thrown inside the callback” 
); 
});
Tests Should be Atomic 
 Execution order cannot be guaranteed! 
 Each test should be independent from one another. 
 QUnit.test() is used to keep test cases atomic.
Async Control : QUnit.asyncTest 
For testing asynchronous code, QUnit.asyncTest will 
automatically stop the test runner and wait for your code 
to call QUnit.start() to continue. 
The following illustrates an asynchronous test that waits 1 
second before resuming 
QUnit.asyncTest( "asynchronous test: one second later!", function( 
assert ) { 
expect( 1 ); 
setTimeout(function() { 
assert.ok( true, "Passed and ready to resume!" ); 
QUnit.start(); }, 1000); 
});
Async Control : QUnit.stop() 
Increase the number of QUnit.start() calls the testrunner 
should wait for before continuing. 
When your async test has multiple exit points, call 
QUnit.stop() multiple times or use the increment 
argument. 
QUnit.test( "a test", function( assert ){ 
QUnit.stop(); 
setTimeout(function(){ 
assert.equals("somedata" , "someExpectedValue" ); 
QUnit.start(); }, 150 ); 
});
Grouping Tests : QUnit.module() 
It groups tests together to keep them logically organized 
and be able to run a specific group of tests on their own. 
All tests that occur after a call to QUnit.module() will be 
grouped into that module. The test names will all be 
preceded by the module name in the test results. 
QUnit.module( "group a" );//tests for module a 
QUnit.module( "group b" );//test for module b
Grouping Tests : QUnit.module() 
QUnit.module() can also be used to extract common code 
from tests within that module. 
The QUnit.module() function takes an optional second 
parameter to define functions to run before and after 
each test within the module 
QUnit.module( "module", { 
setup: function( assert ) 
{//any setup task}, 
teardown: function( assert ) 
{//task to be performed after test completion} 
}); 
QUnit.test( "test with setup and teardown", function() 
{ 
//test cases 
});
Callbacks 
When integrating QUnit into other tools like CI servers, 
use these callbacks as an API to read test results. 
 QUnit.begin() 
 QUnit.done() 
 QUnit.moduleStart() 
 QUnit.moduleDone() 
 QUnit.testStart() 
 QUnit.testDone()
QUnit Test - Suite
Node :QUnit 
1. Install nodejs 
2. Install qunit node module 
npm install qunit 
testrunner.js 
var runner = require("../../node/node_modules/qunit"); 
runner.run({ 
code : "/full/path/to/public/js/main.js", 
tests : "/full/path/to/tests/js/tests.js" 
}); 
Node command 
node tests/js/testrunner.js
Automated Testing 
Install Node 
Using Node Package Manager install Grunt 
Install QUnit module to your project directory(npm install 
qunit) 
└ project 
├ src // plugin source, project files, etc 
├ tests // we'll be working in here mostly 
│ ├ lib 
│ │ ├ jquery-1.x.x.min.js // if you need it (QUnit doesn't) 
│ │ ├ qunit-1.10.0.js 
│ │ └ qunit-1.10.0.css 
│ ├ index.html // our QUnit test specification 
│ └ tests.js // your tests (could be split into multiple files) 
├ Gruntfile.js // you'll create this, we'll get to it shortly 
├ package.json // to specify our project dependencies
Automated Testing (contd.) 
//package.json 
{ 
"name": "projectName", 
"version": "1.0.0", 
"devDependencies": { 
"grunt": "~0.4.1", 
"grunt-contrib-qunit": ">=0.2.1", 
} 
} 
//Gruntfile.js 
module.exports = function(grunt) { 
grunt.initConfig({ 
pkg: grunt.file.readJSON('package.json'), 
taskName: {qunit: { 
all: ['tests/*.html'] 
}} 
}); 
grunt.loadNpmTasks('grunt-contrib-qunit'); 
grunt.registerTask('default', ['qunit']); 
};
Unit Testing JavaScript Code with QUnit Framework

Contenu connexe

Tendances

Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockRobot Media
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first stepsRenato Primavera
 
Adventures In JavaScript Testing
Adventures In JavaScript TestingAdventures In JavaScript Testing
Adventures In JavaScript TestingThomas Fuchs
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and JasmineAngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasminefoxp2code
 
Unit testing with Easymock
Unit testing with EasymockUnit testing with Easymock
Unit testing with EasymockÜrgo Ringo
 
JMockit Framework Overview
JMockit Framework OverviewJMockit Framework Overview
JMockit Framework OverviewMario Peshev
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingLars Thorup
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSKnoldus Inc.
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifePeter Gfader
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test frameworkAbner Chih Yi Huang
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkHumberto Marchezi
 
AngularJS Testing Strategies
AngularJS Testing StrategiesAngularJS Testing Strategies
AngularJS Testing Strategiesnjpst8
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaChristopher Bartling
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkArulalan T
 

Tendances (20)

Unit Testing in iOS
Unit Testing in iOSUnit Testing in iOS
Unit Testing in iOS
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Angular testing
Angular testingAngular testing
Angular testing
 
Adventures In JavaScript Testing
Adventures In JavaScript TestingAdventures In JavaScript Testing
Adventures In JavaScript Testing
 
Mockito intro
Mockito introMockito intro
Mockito intro
 
AngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and JasmineAngularJS Unit Testing w/Karma and Jasmine
AngularJS Unit Testing w/Karma and Jasmine
 
Full Stack Unit Testing
Full Stack Unit TestingFull Stack Unit Testing
Full Stack Unit Testing
 
Unit testing with Easymock
Unit testing with EasymockUnit testing with Easymock
Unit testing with Easymock
 
Jasmine BDD for Javascript
Jasmine BDD for JavascriptJasmine BDD for Javascript
Jasmine BDD for Javascript
 
JMockit Framework Overview
JMockit Framework OverviewJMockit Framework Overview
JMockit Framework Overview
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit Testing
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Testing with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs LifeTesting with VS2010 - A Bugs Life
Testing with VS2010 - A Bugs Life
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
AngularJS Testing Strategies
AngularJS Testing StrategiesAngularJS Testing Strategies
AngularJS Testing Strategies
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 

En vedette

Automated Testing in EmberJS
Automated Testing in EmberJSAutomated Testing in EmberJS
Automated Testing in EmberJSBen Limmer
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingLars Thorup
 
Javascript Unit Testting (PHPBenelux 2011-05-04)
Javascript Unit Testting (PHPBenelux 2011-05-04)Javascript Unit Testting (PHPBenelux 2011-05-04)
Javascript Unit Testting (PHPBenelux 2011-05-04)Tom Van Herreweghe
 
JavaScript Test-Driven Development (TDD) with QUnit
JavaScript Test-Driven Development (TDD) with QUnitJavaScript Test-Driven Development (TDD) with QUnit
JavaScript Test-Driven Development (TDD) with QUnitTasanakorn Phaipool
 
Unit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitUnit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitLars Thorup
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScriptSimon Guest
 

En vedette (8)

Automated Testing in EmberJS
Automated Testing in EmberJSAutomated Testing in EmberJS
Automated Testing in EmberJS
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit Testing
 
Javascript Unit Testting (PHPBenelux 2011-05-04)
Javascript Unit Testting (PHPBenelux 2011-05-04)Javascript Unit Testting (PHPBenelux 2011-05-04)
Javascript Unit Testting (PHPBenelux 2011-05-04)
 
JavaScript Test-Driven Development (TDD) with QUnit
JavaScript Test-Driven Development (TDD) with QUnitJavaScript Test-Driven Development (TDD) with QUnit
JavaScript Test-Driven Development (TDD) with QUnit
 
Unit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitUnit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnit
 
Vuejs testing
Vuejs testingVuejs testing
Vuejs testing
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScript
 

Similaire à Unit Testing JavaScript Code with QUnit Framework

Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4jeresig
 
Technical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnitTechnical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnitArcBlock
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
 
Testing And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionTesting And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionDenard Springle IV
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialAnup Singh
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentationSanjib Dhar
 
05 junit
05 junit05 junit
05 junitmha4
 
Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013Svetlin Nakov
 
Unit testing by Svetlin Nakov
Unit testing by Svetlin NakovUnit testing by Svetlin Nakov
Unit testing by Svetlin Nakovit-tour
 
J unit presentation
J unit presentationJ unit presentation
J unit presentationPriya Sharma
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentAll Things Open
 

Similaire à Unit Testing JavaScript Code with QUnit Framework (20)

Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4Testing, Performance Analysis, and jQuery 1.4
Testing, Performance Analysis, and jQuery 1.4
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
Technical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnitTechnical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnit
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
Testing And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionTesting And Mxunit In ColdFusion
Testing And Mxunit In ColdFusion
 
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit TutorialJAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Angular Unit Testing
Angular Unit TestingAngular Unit Testing
Angular Unit Testing
 
Junit
JunitJunit
Junit
 
05 junit
05 junit05 junit
05 junit
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013Unit Testing - Nakov's Talk @ VarnaConf 2013
Unit Testing - Nakov's Talk @ VarnaConf 2013
 
Unit testing by Svetlin Nakov
Unit testing by Svetlin NakovUnit testing by Svetlin Nakov
Unit testing by Svetlin Nakov
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 

Unit Testing JavaScript Code with QUnit Framework

  • 2. Points to Discuss  Unit Testing & Test Driven Development  QUnit  QUnit API  Automated Testing (a brief introduction)
  • 3. Unit Testing  In computer programming, unit testing is a procedure used to validate that individual modules or units of source code are working properly.  Unit testing is used for (i) Test Driven Development (ii) Fixing bugs (iii) Regression testing
  • 4. Test Driven Development  Test-Driven Development (TDD) is a computer programming technique that involves repeatedly first writing a test case and then implementing only the code necessary to pass the test.  Test-driven development is a method of designing software, not merely a method of testing.
  • 6. Fixing bugs/Regression Testing  Fixing bugs  Regression testing
  • 7. What do you need?  A Unit Testing framework  Development Environment
  • 8. QUnit  QUnit is a powerful, easy-to-use JavaScript unit testing framework.  It's used by the jQuery, jQuery UI and jQuery Mobile projects and is capable of testing any generic JavaScript code, including itself!  Supports the same browsers as jQuery 1.x. That's IE6+ and Current - 1 for Chrome, Firefox, Safari and Opera.
  • 9. Getting Started  Just qunit.js, qunit.css, and a little bit of HTML  A Quick Demo: http://jsfiddle.net/akankshaaro/rq31v84h/
  • 10. QUnit API  Test  Assert  Async Control  Callback
  • 11. Test : QUnit.test( name, test )  Adds a test to run.  Testing the most common, synchronous code Qunit.test(“name of the test”, function() { //write down the assertions }); function: Function to close over assertions
  • 12. Test : expect() Specify how many assertions are expected to run within a test. If the number of assertions run does not match the expected count, the test will fail. test(”expected assertions", function() { expect( 2 ); //two assertions are expected });
  • 13. Asserts  ok  Equal  notEqual  strictEqual  notStrictEqual  deepEqual  notDeepEqual  throws
  • 14. Assert - ok() ok( state, message ) A boolean check, passes if the first argument is truthy. test("ok”, function() { expect(3); ok(true, "passes because true is true"); ok(1, "passes because 1 is truthy"); ok("", "fails because empty string is not truthy"); });
  • 15. Assert - equal() equal( actual, expected, message ) A comparison assertion that passes if actual == expected. test("equal”, function() { expect(3); var actual = 5 - 4; equal(actual, 1, "passes because 1 == 1"); equal(actual, true, "passes because 1 == true"); equal(actual, false, "fails because 1 != false"); });
  • 16. Assert - notEqual() notEqual( actual, expected, message ) A comparison assertion that passes if actual != expected. test("notEqual”, function() { expect(3); var actual = 5 - 4; notEqual(actual, 0, "passes because 1 != 0"); notEqual(actual, false, "passes because 1 != false"); notEqual(actual, true, "fails because 1 == true"); });
  • 17. Assert - strictEqual() strictEqual( actual, expected, message ) A comparison assertion that passes if actual === expected. test("notEqual”, function() { expect(3); var actual = 5 - 4; strictEqual(actual, 1, "passes because 1 === 1"); strictEqual(actual, true, "fails because 1 !== true"); strictEqual(actual, false, "fails because 1 !== false"); });
  • 18. Assert - notStrictEqual() notStrictEqual( actual, expected, message ) A comparison assertion that passes if actual !== expected. test("notStrictEqual”, function() { expect(3); var actual = 5 - 4; notStrictEqual(actual, 1, "fails because 1 === 1"); notStrictEqual(actual, true, "passes because 1 !== true"); notStrictEqual(actual, false, "passes because 1 !== false"); });
  • 19. Assert - deepEqual () deepEqual( actual, expected, message ) Recursive comparison assertion, working on primitives, arrays and objects, using ===. test("deepEqual”, function() { expect(3); var actual = {a: 1}; equal( actual, {a: 1}, "fails because objects are different"); deepEqual(actual, {a: 1}, "passes because objects are equivalent"); deepEqual(actual, {a: "1"}, "fails because '1' !== 1"); });
  • 20. Assert - notDeepEqual() notDeepEqual( actual, expected, message ) Recursive comparison assertion. The result of deepEqual, inverted. test("notDeepEqual”, function() { expect(3); var actual = {a: 1}; notEqual( actual, {a: 1}, "passes because objects are different"); notDeepEqual(actual, {a: 1}, "fails because objects are equivalent"); notDeepEqual(actual, {a: "1"}, "passes because '1' !== 1"); });
  • 21. Assert - throws() Assertion to test if a callback throws an exception when run and optionally compare the thrown error. test("throws”, function() { expect(3); throws( function() { throw new Error("Look me, I'm an error!"); }, "passes because an error is thrown inside the callback” ); throws( function() { x // ReferenceError: x is not defined }, "passes because an error is thrown inside the callback” ); throws ( function() { var a = 1; }, "fails because no error is thrown inside the callback” ); });
  • 22. Tests Should be Atomic  Execution order cannot be guaranteed!  Each test should be independent from one another.  QUnit.test() is used to keep test cases atomic.
  • 23. Async Control : QUnit.asyncTest For testing asynchronous code, QUnit.asyncTest will automatically stop the test runner and wait for your code to call QUnit.start() to continue. The following illustrates an asynchronous test that waits 1 second before resuming QUnit.asyncTest( "asynchronous test: one second later!", function( assert ) { expect( 1 ); setTimeout(function() { assert.ok( true, "Passed and ready to resume!" ); QUnit.start(); }, 1000); });
  • 24. Async Control : QUnit.stop() Increase the number of QUnit.start() calls the testrunner should wait for before continuing. When your async test has multiple exit points, call QUnit.stop() multiple times or use the increment argument. QUnit.test( "a test", function( assert ){ QUnit.stop(); setTimeout(function(){ assert.equals("somedata" , "someExpectedValue" ); QUnit.start(); }, 150 ); });
  • 25. Grouping Tests : QUnit.module() It groups tests together to keep them logically organized and be able to run a specific group of tests on their own. All tests that occur after a call to QUnit.module() will be grouped into that module. The test names will all be preceded by the module name in the test results. QUnit.module( "group a" );//tests for module a QUnit.module( "group b" );//test for module b
  • 26. Grouping Tests : QUnit.module() QUnit.module() can also be used to extract common code from tests within that module. The QUnit.module() function takes an optional second parameter to define functions to run before and after each test within the module QUnit.module( "module", { setup: function( assert ) {//any setup task}, teardown: function( assert ) {//task to be performed after test completion} }); QUnit.test( "test with setup and teardown", function() { //test cases });
  • 27. Callbacks When integrating QUnit into other tools like CI servers, use these callbacks as an API to read test results.  QUnit.begin()  QUnit.done()  QUnit.moduleStart()  QUnit.moduleDone()  QUnit.testStart()  QUnit.testDone()
  • 28. QUnit Test - Suite
  • 29. Node :QUnit 1. Install nodejs 2. Install qunit node module npm install qunit testrunner.js var runner = require("../../node/node_modules/qunit"); runner.run({ code : "/full/path/to/public/js/main.js", tests : "/full/path/to/tests/js/tests.js" }); Node command node tests/js/testrunner.js
  • 30. Automated Testing Install Node Using Node Package Manager install Grunt Install QUnit module to your project directory(npm install qunit) └ project ├ src // plugin source, project files, etc ├ tests // we'll be working in here mostly │ ├ lib │ │ ├ jquery-1.x.x.min.js // if you need it (QUnit doesn't) │ │ ├ qunit-1.10.0.js │ │ └ qunit-1.10.0.css │ ├ index.html // our QUnit test specification │ └ tests.js // your tests (could be split into multiple files) ├ Gruntfile.js // you'll create this, we'll get to it shortly ├ package.json // to specify our project dependencies
  • 31. Automated Testing (contd.) //package.json { "name": "projectName", "version": "1.0.0", "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-qunit": ">=0.2.1", } } //Gruntfile.js module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), taskName: {qunit: { all: ['tests/*.html'] }} }); grunt.loadNpmTasks('grunt-contrib-qunit'); grunt.registerTask('default', ['qunit']); };