SlideShare a Scribd company logo
Efficient JavaScript Unit Testing
Hazem Saleh
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
About Me
• Staff Software Engineer / Consulting IT Specialist in IBM Egypt,
 Cairo Lab, SWG Services.
• Web 2.0 and WebSphere Portal Subject Matter Expert.
• Apache MyFaces committer.
• Founder of GMaps4JSF.
• Author of the definitive guide to Apache MyFaces book, and
 reviewer of many other books.
• DeveloperWorks Contributing Author.
• International Technical Speaker in both local and international
 conferences (such as JavaOne).

• Blog: http://www.technicaladvices.com
• Twitter: http://www.twitter.com/hazems
Developers Life without Unit testing

Complex integration between the system components.
Developers Life without Unit testing

Unmanaged number of new/regression defects especially when
the system complexity increases.
Developers Life without Unit testing


Low application quality.
Developers Life without Unit testing


Long testing cycle.
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
What is unit testing and why?


Unit testing means testing every component in the system in an
Independent way to ensure that it is working properly.


Unit testing helps in detecting BUGGY components in the early
stages of the project.


A test case is a set of steps and conditions to test the features
and functionalities of the application.
What is unit testing and why?

Integration is much simplified.

Defects are managed. Regression defects should not happen if the defect is
resolved by creating a new test case.

Test cases can be a good reference for system documentation.

Test cases can improve the system design and be the basis of code refactoring.

Application quality increases.

Testing cycle is reduced.
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
Current Complexities in testing JavaScript code



   Requires a lot of time to test on all the browsers.

   JavaScript code that runs on a specific browser does not
   necessarily mean that it will work on other browsers.




    Supporting a new browser means allocating a new budget for
    testing the system again on this new browser and for the
    new/regression defects fixes.
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
Requirements of a good JavaScript unit testing
                    tool

                            JavaScript unit
                              testing tool

Can execute across all                               Fast Test case
the browsers over                                    execution.
 all the platforms.



              Easy setup.                     Integration with
                                                   IDEs.



 Easy configuration.                             Integration with build
                                                 management tools.
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
What is JsTestDriver

  One of the best Open source JavaScript testing tools.

  Meets all of the previous requirements and more:

             Supports all the browsers / all platforms. ✓

             Easy setup and configuration. ✓

             Fast Test case execution. ✓

             Integration with IDEs and build management tools. ✓
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
                                Configuration.
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
JsTestDriver Architecture
JsTestDriver configuration


1   Run the test cases using the following command line
JsTestDriver configuration


1   2   Download the jsTestDriver latest jars from


         http://code.google.com/p/js-test-driver/downloads/list
JsTestDriver configuration

               Create the jsTestDriver.conf file (under the JS folder)
1   2   3      with the following initial content:



            server: http://localhost:9876
            load:
             - js-src/*.js
             - js-test/*.js
JsTestDriver configuration

                      Start the server using the following command
1    2    3    4      line




    java -jar JsTestDriver-1.3.2.jar
    Optional parameters
      [--port 9876]
      [--browser
      “{PATH}firefox.exe","{PATH}iexplore.exe","{P
      ATH}Safari.exe"]
JsTestDriver configuration

                               Run the test cases using the following
 1     2    3     4     5      command line



java -jar JsTestDriver-1.3.2.jar --tests all
.........
Total 9 tests (Passed: 9; Fails: 0; Errors: 0) (16.00 ms)
  Firefox 9.0.1 Windows: Run 3 tests (Passed: 3; Fails: 0; Errors 0)
(3.00 ms)
  Safari 534.52.7 Windows: Run 3 tests (Passed: 3; Fails: 0; Errors
0) (4.00 ms)
  Microsoft Internet Explorer 7.0 Windows: Run 3 tests (Passed: 3;
Fails: 0; Errors 0) (16.00 ms)
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
    JsTestDriver Eclipse plugin.
I
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
JsTestDriver Eclipse plugin


Instead of using command lines for starting the server and running the test
cases, you can directly use the jsTestDriver Eclipse plugin.



To install the JsTestDriver Eclipse plugin install the plugin from the following
URL : http://js-test-driver.googlecode.com/svn/update/
JsTestDriver Eclipse plugin
JsTestDriver Eclipse plugin
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E
    JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
Writing a JavaScript TestCase
ApplicationUtilTest = TestCase("ApplicationUtilTest");

ApplicationUtilTest.prototype.setUp = function () {
   /*:DOC += ...HTML fragment code goes here (single root) ...*/
};

ApplicationUtilTest.prototype.testMethod1 = function () {
  … validate using the jsTestDriver constructs …
}

ApplicationUtilTest.prototype.testMethod2 = function () {
  … validate using the jsTestDriver constructs …
}

...
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E   JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
JsTestDriver common constructs

    fail("msg")

    assertTrue("msg", actual)

    assertFalse("msg", actual)

    assertSame("msg", expected, actual)

    assertNotSame("msg", expected, actual)

    assertNull("msg", actual)

    assertNotNull("msg", actual)
DEMO
Let’s write synchronous
    JS Test cases …
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E   JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility
    Generating reports from test cases.
Writing asynchronous JavaScript TestCase


JsTestDriver provides AsyncTestCase object for
performing asynchronous JavaScript unit testing.


In order to test the asynchronous operations, JSTD
provides queues.


Each queue contains set of callbacks for testing the
Asynchronous system.
Writing asynchronous JavaScript TestCase


There are two types of callbacks:
• Normal callback: MUST be called to verify the Ajax
  operation success
• Error callback: Represents the error path. If it is
  called, then the test should fail.


The test runner does not move to the next queue until
the current queue executes all of its normal callbacks. If
a specific normal callback is not called for a specific
amount of time (30 seconds), the test fails.
Writing asynchronous JavaScript TestCase
WeatherClientTest = AsyncTestCase("WeatherClientTest");


WeatherClientTest.prototype.setUp = function () {
     /*:DOC += <form><div id="weatherInformation"></div></form>*/
};
WeatherClientTest.prototype.testGetWeatherConditionForCairo = function(queue) {
     queue.call('Testing getting weather Information ...', function(callbacks) {
           var weatherClient = new appnamespace.WeatherClient();


           var successCallBack = callbacks.add(function(weatherClient) {
                       weatherClient.displayWeatherInformation(weatherClient);
             });


           var failureCallBack = callbacks.addErrback('Unable to retrieve weather information');
           // call asynchronous API
            weatherClient.getWeatherCondition("1521894", "weatherInformation",
                                   successCallBack,
                                   failureCallBack);
     });
};
WeatherClientTest.prototype.testGetWeatherConditionForParis = function(queue) {
//Step 2 …
};
DEMO
Let’s write Asynchronous JS
       Test cases …
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E   JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility

    Generating reports from test cases.
JsTestDriver Compatibility


JsTestDriver is not only a JavaScript unit testing
framework BUT it is a test runner for many other
JavaScript unit testing frameworks.

JsTestDriver is compatibility with the following
JavaScript unit testing frameworks through adapters:
   • Jasmine
   • Yahoo UI Test
   • QUnit
JsTestDriver Compatibility


In order to run the previous unit testing frameworks
on the top of the JSTD test runner. You need to
configure the framework adapter and source before
the test files as follows:

 server: http://localhost:9876

 load:
  - jasmine/lib/jasmine-1.1.0/jasmine.js
  - jasmine/lib/adapter/JasmineAdapter.js
  - js-src/Basics.js
  - js-test/BasicsSpec.js
DEMO
Running Jasmine Test cases
   on the top of JSTD
Developers Life without Unit testing.

    What is unit testing? and why?

    Current Complexities in testing JavaScript code.

    Requirements of a good JavaScript unit testing tool.
O
U   What is JsTestDriver.
T   JsTestDriver Architecture & Configuration
L
I   JsTestDriver Eclipse plugin.
N   Writing a JavaScript TestCase.
E   JsTestDriver common constructs.

    Writing asynchronous JavaScript TestCase.

    JsTestDriver Compatibility

    Generating reports from test cases.
Generating reports from test cases


JSTD can generate code coverage files.


Code coverage describes how much the source code is tested.


Coverage Criteria:

                     Function coverage


                                 statement coverage

                                            Branch
                                                 coverage
JsTestDriver
  can generate code coverage
for your JavaScript code using the
      code coverage plugin.
Generating reports from test cases
Generating reports from test cases


Unfortunately   JsTestDriver does not generate HTML reports directly,

                       JsTestDriver generates the test coverage files in LCOV
                       format.

                You can generate the test reports using the LCOV
                visualizer tool:

                       http://ltp.sourceforge.net/coverage/lcov.php
Generating reports from test cases


       The JsTestDriver LCOV file name is usually:

              <config filename>-coverage.dat
              (jsTestDriver.conf-coverage.dat)

       To generate the report from the LCOV file using the
       LCOV visualizer tool:

              genhtml jsTestDriver.conf-coverage.dat
Conclusion
Conclusion

  Testing JavaScript code is important for increasing the
  application quality and for speeding up fixing defects and
  minimizing the number of regression defects.


  Good JavaScript tool should be configurable, easy to use,
  and working with all the browsers.


  JsTestDriver is one of the most powerful JavaScript unit
  testing tools that can be used for testing both synchronous
  and asynchronous JavaScript code on all the browsers.
Efficient JavaScript Unit Testing, May 2012

More Related Content

What's hot (20)

ZIP
Automated Frontend Testing
Neil Crosby
 
PDF
Testing Web Applications
Seth McLaughlin
 
PDF
Introducing Playwright's New Test Runner
Applitools
 
PDF
Join the darkside: Selenium testing with Nightwatch.js
Seth McLaughlin
 
PDF
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Cogapp
 
PPTX
Nightwatch JS for End to End Tests
Sriram Angajala
 
PDF
PHP Unit Testing in Yii
IlPeach
 
PPTX
Automation testing with Drupal 8
nagpalprachi
 
PDF
Testing with Codeception (Webelement #30)
Adam Štipák
 
PPTX
Protractor Tutorial Quality in Agile 2015
Andrew Eisenberg
 
PDF
Automated Testing in Angular Slides
Jim Lynch
 
PPTX
CI / CD w/ Codeception
Tudor Barbu
 
PDF
Test all the things! Automated testing with Drupal 8
Sam Becker
 
PDF
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Ondřej Machulda
 
PDF
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Ondřej Machulda
 
PDF
AngularJS and Protractor
Filipe Falcão
 
PDF
UI Testing Best Practices - An Expected Journey
Oren Farhi
 
PDF
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Codemotion
 
PDF
Codeception introduction and use in Yii
IlPeach
 
PDF
Node.js and Selenium Webdriver, a journey from the Java side
Mek Srunyu Stittri
 
Automated Frontend Testing
Neil Crosby
 
Testing Web Applications
Seth McLaughlin
 
Introducing Playwright's New Test Runner
Applitools
 
Join the darkside: Selenium testing with Nightwatch.js
Seth McLaughlin
 
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Cogapp
 
Nightwatch JS for End to End Tests
Sriram Angajala
 
PHP Unit Testing in Yii
IlPeach
 
Automation testing with Drupal 8
nagpalprachi
 
Testing with Codeception (Webelement #30)
Adam Štipák
 
Protractor Tutorial Quality in Agile 2015
Andrew Eisenberg
 
Automated Testing in Angular Slides
Jim Lynch
 
CI / CD w/ Codeception
Tudor Barbu
 
Test all the things! Automated testing with Drupal 8
Sam Becker
 
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Ondřej Machulda
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Ondřej Machulda
 
AngularJS and Protractor
Filipe Falcão
 
UI Testing Best Practices - An Expected Journey
Oren Farhi
 
Carmen Popoviciu - Protractor styleguide | Codemotion Milan 2015
Codemotion
 
Codeception introduction and use in Yii
IlPeach
 
Node.js and Selenium Webdriver, a journey from the Java side
Mek Srunyu Stittri
 

Viewers also liked (20)

KEY
Agile JavaScript Testing
Scott Becker
 
PDF
Maintainable JavaScript 2011
Nicholas Zakas
 
PDF
Scalable JavaScript Application Architecture
Nicholas Zakas
 
PDF
AngularJS Deep Dives (NYC GDG Apr 2013)
Nitya Narasimhan
 
PPTX
React in Native Apps - Meetup React - 20150409
Minko3D
 
PDF
The Art of AngularJS - DeRailed 2014
Matt Raible
 
PDF
React JS and why it's awesome
Andrew Hull
 
PPTX
Unit testing js
Alexandra Morozova
 
KEY
Intro To Sammy
Brandon Aaron
 
KEY
All About Sammy
Scott Becker
 
PPTX
[AnDevCon 2016] Mutation Testing for Android
Hazem Saleh
 
PDF
Efficient JavaScript Unit Testing, JavaOne China 2013
Hazem Saleh
 
PPTX
모바일 컨버전스 5가지 패러다임 업로드용
misia85
 
PPTX
Unit Testing TypeScript
Daniel Jimenez Garcia
 
PDF
Enterprise JavaScript Error Handling (Ajax Experience 2008)
Nicholas Zakas
 
PPTX
Becoming Node.js ninja on Cloud Foundry
Raja Rao DV
 
KEY
Meteor 0.3.6 Preview
Juntai Park
 
PDF
제2회 hello world 오픈세미나 hello world-raphael차트
NAVER D2
 
PDF
다이내믹 스타일시트 언어 Less framework 활용 by yamoo9
yamoo9
 
PDF
Haml And Sass In 15 Minutes
Patrick Crowley
 
Agile JavaScript Testing
Scott Becker
 
Maintainable JavaScript 2011
Nicholas Zakas
 
Scalable JavaScript Application Architecture
Nicholas Zakas
 
AngularJS Deep Dives (NYC GDG Apr 2013)
Nitya Narasimhan
 
React in Native Apps - Meetup React - 20150409
Minko3D
 
The Art of AngularJS - DeRailed 2014
Matt Raible
 
React JS and why it's awesome
Andrew Hull
 
Unit testing js
Alexandra Morozova
 
Intro To Sammy
Brandon Aaron
 
All About Sammy
Scott Becker
 
[AnDevCon 2016] Mutation Testing for Android
Hazem Saleh
 
Efficient JavaScript Unit Testing, JavaOne China 2013
Hazem Saleh
 
모바일 컨버전스 5가지 패러다임 업로드용
misia85
 
Unit Testing TypeScript
Daniel Jimenez Garcia
 
Enterprise JavaScript Error Handling (Ajax Experience 2008)
Nicholas Zakas
 
Becoming Node.js ninja on Cloud Foundry
Raja Rao DV
 
Meteor 0.3.6 Preview
Juntai Park
 
제2회 hello world 오픈세미나 hello world-raphael차트
NAVER D2
 
다이내믹 스타일시트 언어 Less framework 활용 by yamoo9
yamoo9
 
Haml And Sass In 15 Minutes
Patrick Crowley
 
Ad

Similar to Efficient JavaScript Unit Testing, May 2012 (20)

PDF
Efficient JavaScript Unit Testing, March 2013
Hazem Saleh
 
PPT
JavaScript Unit Testing
Christian Johansen
 
PPT
Pragmatic Parallels: Java and JavaScript
davejohnson
 
ODP
S313352 optimizing java device testing with automatic feature discovering
romanovfedor
 
DOCX
Test Driven Development
Anand Kumar Rajana
 
PDF
[FullStack NYC 2019] Effective Unit Tests for JavaScript
Hazem Saleh
 
PPTX
Qa process
Aila Bogasieru
 
PDF
Quest to the best test automation for low code development platform kherrazi ...
Rachid Kherrazi
 
PPTX
Qa process
Aila Bogasieru
 
PDF
Testing Angular
Lilia Sfaxi
 
PDF
Lesson 2
Andrii Trybynenko
 
PPTX
Angular Unit testing.pptx
RiyaBangera
 
PPTX
Java script unit testing
Mats Bryntse
 
PPTX
Test driven development in .Net - 2010 + Eclipse
UTC Fire & Security
 
PPT
Test strategy for web development
alice yang
 
PPTX
Protractor
Artem Chechoro
 
PPTX
Introduction to Protractor - Habilelabs
HabileLabs
 
PPTX
Deep Dive Modern Apps Lifecycle with Visual Studio 2012: How to create cross ...
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
PDF
Streamline Testing: Transition from Manual to Automation with Selenium & C#
digitaljignect
 
PDF
Streamline Testing: Transition from Manual to Automation with Selenium & C#
digitaljignect
 
Efficient JavaScript Unit Testing, March 2013
Hazem Saleh
 
JavaScript Unit Testing
Christian Johansen
 
Pragmatic Parallels: Java and JavaScript
davejohnson
 
S313352 optimizing java device testing with automatic feature discovering
romanovfedor
 
Test Driven Development
Anand Kumar Rajana
 
[FullStack NYC 2019] Effective Unit Tests for JavaScript
Hazem Saleh
 
Qa process
Aila Bogasieru
 
Quest to the best test automation for low code development platform kherrazi ...
Rachid Kherrazi
 
Qa process
Aila Bogasieru
 
Testing Angular
Lilia Sfaxi
 
Angular Unit testing.pptx
RiyaBangera
 
Java script unit testing
Mats Bryntse
 
Test driven development in .Net - 2010 + Eclipse
UTC Fire & Security
 
Test strategy for web development
alice yang
 
Protractor
Artem Chechoro
 
Introduction to Protractor - Habilelabs
HabileLabs
 
Deep Dive Modern Apps Lifecycle with Visual Studio 2012: How to create cross ...
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
Streamline Testing: Transition from Manual to Automation with Selenium & C#
digitaljignect
 
Streamline Testing: Transition from Manual to Automation with Selenium & C#
digitaljignect
 
Ad

More from Hazem Saleh (14)

PDF
Mockito 2.x Migration - Droidcon UK 2018
Hazem Saleh
 
PDF
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
PPTX
[ApacheCon 2016] Advanced Apache Cordova
Hazem Saleh
 
PPTX
[Devoxx Morocco 2015] Apache Cordova In Action
Hazem Saleh
 
PPTX
Apache Cordova In Action
Hazem Saleh
 
PPTX
[JavaLand 2015] Developing JavaScript Mobile Apps Using Apache Cordova
Hazem Saleh
 
PPTX
[JMaghreb 2014] Developing JavaScript Mobile Apps Using Apache Cordova
Hazem Saleh
 
PDF
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014
Hazem Saleh
 
PDF
Dojo >= 1.7 Kickstart
Hazem Saleh
 
PDF
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Hazem Saleh
 
PDF
JSF Mashups in Action
Hazem Saleh
 
PDF
JavaScript tools
Hazem Saleh
 
PDF
[JavaOne 2010] Abstract Mashups for Enterprise Java
Hazem Saleh
 
PDF
GMaps4JSF
Hazem Saleh
 
Mockito 2.x Migration - Droidcon UK 2018
Hazem Saleh
 
JavaScript Unit Testing with an Angular 5.x Use Case 101
Hazem Saleh
 
[ApacheCon 2016] Advanced Apache Cordova
Hazem Saleh
 
[Devoxx Morocco 2015] Apache Cordova In Action
Hazem Saleh
 
Apache Cordova In Action
Hazem Saleh
 
[JavaLand 2015] Developing JavaScript Mobile Apps Using Apache Cordova
Hazem Saleh
 
[JMaghreb 2014] Developing JavaScript Mobile Apps Using Apache Cordova
Hazem Saleh
 
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014
Hazem Saleh
 
Dojo >= 1.7 Kickstart
Hazem Saleh
 
Efficient JavaScript Unit Testing (Chinese Version), JavaOne China 2013
Hazem Saleh
 
JSF Mashups in Action
Hazem Saleh
 
JavaScript tools
Hazem Saleh
 
[JavaOne 2010] Abstract Mashups for Enterprise Java
Hazem Saleh
 
GMaps4JSF
Hazem Saleh
 

Recently uploaded (20)

PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 

Efficient JavaScript Unit Testing, May 2012

  • 1. Efficient JavaScript Unit Testing Hazem Saleh
  • 2. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 3. About Me • Staff Software Engineer / Consulting IT Specialist in IBM Egypt, Cairo Lab, SWG Services. • Web 2.0 and WebSphere Portal Subject Matter Expert. • Apache MyFaces committer. • Founder of GMaps4JSF. • Author of the definitive guide to Apache MyFaces book, and reviewer of many other books. • DeveloperWorks Contributing Author. • International Technical Speaker in both local and international conferences (such as JavaOne). • Blog: http://www.technicaladvices.com • Twitter: http://www.twitter.com/hazems
  • 4. Developers Life without Unit testing Complex integration between the system components.
  • 5. Developers Life without Unit testing Unmanaged number of new/regression defects especially when the system complexity increases.
  • 6. Developers Life without Unit testing Low application quality.
  • 7. Developers Life without Unit testing Long testing cycle.
  • 8. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 9. What is unit testing and why? Unit testing means testing every component in the system in an Independent way to ensure that it is working properly. Unit testing helps in detecting BUGGY components in the early stages of the project. A test case is a set of steps and conditions to test the features and functionalities of the application.
  • 10. What is unit testing and why? Integration is much simplified. Defects are managed. Regression defects should not happen if the defect is resolved by creating a new test case. Test cases can be a good reference for system documentation. Test cases can improve the system design and be the basis of code refactoring. Application quality increases. Testing cycle is reduced.
  • 11. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 12. Current Complexities in testing JavaScript code Requires a lot of time to test on all the browsers. JavaScript code that runs on a specific browser does not necessarily mean that it will work on other browsers. Supporting a new browser means allocating a new budget for testing the system again on this new browser and for the new/regression defects fixes.
  • 13. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 14. Requirements of a good JavaScript unit testing tool JavaScript unit testing tool Can execute across all Fast Test case the browsers over execution. all the platforms. Easy setup. Integration with IDEs. Easy configuration. Integration with build management tools.
  • 15. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 16. What is JsTestDriver One of the best Open source JavaScript testing tools. Meets all of the previous requirements and more: Supports all the browsers / all platforms. ✓ Easy setup and configuration. ✓ Fast Test case execution. ✓ Integration with IDEs and build management tools. ✓
  • 17. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration Configuration. L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 19. JsTestDriver configuration 1 Run the test cases using the following command line
  • 20. JsTestDriver configuration 1 2 Download the jsTestDriver latest jars from http://code.google.com/p/js-test-driver/downloads/list
  • 21. JsTestDriver configuration Create the jsTestDriver.conf file (under the JS folder) 1 2 3 with the following initial content: server: http://localhost:9876 load: - js-src/*.js - js-test/*.js
  • 22. JsTestDriver configuration Start the server using the following command 1 2 3 4 line java -jar JsTestDriver-1.3.2.jar Optional parameters [--port 9876] [--browser “{PATH}firefox.exe","{PATH}iexplore.exe","{P ATH}Safari.exe"]
  • 23. JsTestDriver configuration Run the test cases using the following 1 2 3 4 5 command line java -jar JsTestDriver-1.3.2.jar --tests all ......... Total 9 tests (Passed: 9; Fails: 0; Errors: 0) (16.00 ms) Firefox 9.0.1 Windows: Run 3 tests (Passed: 3; Fails: 0; Errors 0) (3.00 ms) Safari 534.52.7 Windows: Run 3 tests (Passed: 3; Fails: 0; Errors 0) (4.00 ms) Microsoft Internet Explorer 7.0 Windows: Run 3 tests (Passed: 3; Fails: 0; Errors 0) (16.00 ms)
  • 24. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L JsTestDriver Eclipse plugin. I N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 25. JsTestDriver Eclipse plugin Instead of using command lines for starting the server and running the test cases, you can directly use the jsTestDriver Eclipse plugin. To install the JsTestDriver Eclipse plugin install the plugin from the following URL : http://js-test-driver.googlecode.com/svn/update/
  • 28. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 29. Writing a JavaScript TestCase ApplicationUtilTest = TestCase("ApplicationUtilTest"); ApplicationUtilTest.prototype.setUp = function () { /*:DOC += ...HTML fragment code goes here (single root) ...*/ }; ApplicationUtilTest.prototype.testMethod1 = function () { … validate using the jsTestDriver constructs … } ApplicationUtilTest.prototype.testMethod2 = function () { … validate using the jsTestDriver constructs … } ...
  • 30. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 31. JsTestDriver common constructs fail("msg") assertTrue("msg", actual) assertFalse("msg", actual) assertSame("msg", expected, actual) assertNotSame("msg", expected, actual) assertNull("msg", actual) assertNotNull("msg", actual)
  • 32. DEMO Let’s write synchronous JS Test cases …
  • 33. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 34. Writing asynchronous JavaScript TestCase JsTestDriver provides AsyncTestCase object for performing asynchronous JavaScript unit testing. In order to test the asynchronous operations, JSTD provides queues. Each queue contains set of callbacks for testing the Asynchronous system.
  • 35. Writing asynchronous JavaScript TestCase There are two types of callbacks: • Normal callback: MUST be called to verify the Ajax operation success • Error callback: Represents the error path. If it is called, then the test should fail. The test runner does not move to the next queue until the current queue executes all of its normal callbacks. If a specific normal callback is not called for a specific amount of time (30 seconds), the test fails.
  • 36. Writing asynchronous JavaScript TestCase WeatherClientTest = AsyncTestCase("WeatherClientTest"); WeatherClientTest.prototype.setUp = function () { /*:DOC += <form><div id="weatherInformation"></div></form>*/ }; WeatherClientTest.prototype.testGetWeatherConditionForCairo = function(queue) { queue.call('Testing getting weather Information ...', function(callbacks) { var weatherClient = new appnamespace.WeatherClient(); var successCallBack = callbacks.add(function(weatherClient) { weatherClient.displayWeatherInformation(weatherClient); }); var failureCallBack = callbacks.addErrback('Unable to retrieve weather information'); // call asynchronous API weatherClient.getWeatherCondition("1521894", "weatherInformation", successCallBack, failureCallBack); }); }; WeatherClientTest.prototype.testGetWeatherConditionForParis = function(queue) { //Step 2 … };
  • 37. DEMO Let’s write Asynchronous JS Test cases …
  • 38. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 39. JsTestDriver Compatibility JsTestDriver is not only a JavaScript unit testing framework BUT it is a test runner for many other JavaScript unit testing frameworks. JsTestDriver is compatibility with the following JavaScript unit testing frameworks through adapters: • Jasmine • Yahoo UI Test • QUnit
  • 40. JsTestDriver Compatibility In order to run the previous unit testing frameworks on the top of the JSTD test runner. You need to configure the framework adapter and source before the test files as follows: server: http://localhost:9876 load: - jasmine/lib/jasmine-1.1.0/jasmine.js - jasmine/lib/adapter/JasmineAdapter.js - js-src/Basics.js - js-test/BasicsSpec.js
  • 41. DEMO Running Jasmine Test cases on the top of JSTD
  • 42. Developers Life without Unit testing. What is unit testing? and why? Current Complexities in testing JavaScript code. Requirements of a good JavaScript unit testing tool. O U What is JsTestDriver. T JsTestDriver Architecture & Configuration L I JsTestDriver Eclipse plugin. N Writing a JavaScript TestCase. E JsTestDriver common constructs. Writing asynchronous JavaScript TestCase. JsTestDriver Compatibility Generating reports from test cases.
  • 43. Generating reports from test cases JSTD can generate code coverage files. Code coverage describes how much the source code is tested. Coverage Criteria: Function coverage statement coverage Branch coverage
  • 44. JsTestDriver can generate code coverage for your JavaScript code using the code coverage plugin.
  • 46. Generating reports from test cases Unfortunately JsTestDriver does not generate HTML reports directly, JsTestDriver generates the test coverage files in LCOV format. You can generate the test reports using the LCOV visualizer tool: http://ltp.sourceforge.net/coverage/lcov.php
  • 47. Generating reports from test cases The JsTestDriver LCOV file name is usually: <config filename>-coverage.dat (jsTestDriver.conf-coverage.dat) To generate the report from the LCOV file using the LCOV visualizer tool: genhtml jsTestDriver.conf-coverage.dat
  • 49. Conclusion Testing JavaScript code is important for increasing the application quality and for speeding up fixing defects and minimizing the number of regression defects. Good JavaScript tool should be configurable, easy to use, and working with all the browsers. JsTestDriver is one of the most powerful JavaScript unit testing tools that can be used for testing both synchronous and asynchronous JavaScript code on all the browsers.