July 10, 2014

625 Views

Automation of JavaScript unit test cases generation and execution

JavaScript language has become increasingly popular in the past few years and new techniques and libraries have frequently been released concerning it. JavaScript is not only an object-oriented language, but a dynamically typed one; so, it’s difficult to find a system that can test JavaScript.

There are many software/unit testing tools currently available, but most of them are only targeted towards testing of static/server/desktop-based code. There are no ways to test dynamic/Client code, which is equally important, due to the advances in client-based presentation requirements and the development of cross-platform software using client scripting techniques. Moreover, testing tools require the execution of complete test cases while verifying them. They do not provide an automated unit testing solution that can handle the generation of method call sequence and dynamic instance. Most of the systems focus on static procedural languages and numerical input data types.

Let us consider a solution that provides automated unit test case creation for JavaScript-based applications like the Web, mobile, tablets and native applications. The illustration below shows an integrated unit test case creator, unit test runner, and unit test result viewer, and also enables automated unit test creation and execution without any manual intervention.

Let’s takes a look at how this solution works:

  • Source code Parser will parse code using the regular expression to get the JavaScript source code.
  • JS Parser and Interpreter will analyze the JS source code and process to construct JS Dom.
  • Using JS Dom, the JS unit test case generator will be responsible for generating the JS unit test case file using the predefined unit test case template on the predefined location or network storage. It will also determine the dependencies required for execution of unit test cases, which can be listed as below.
    • Unit test case framework
    • External JS script files
    • Target JS code script files
  • The JS unit test case Execution Engine will combine the unit testing framework and one or more supported target codes to provide a simple, flexible and pluggable unit testing environment required for running JS unit tests. It will output the test result, which will contain the result of each JS unit test case - whether it has passed or failed - with the defined expected result. The output reports are available as web-based html reports.
  • The unit test case Web Report will provide a detailed view of the test case result with stack trace; the details available in this report are given below:
    • Test execution time
    • Passed unit test count
    • Failed unit test count
    • Assertion passed/ failed count
    • Expected and actual result for each failed test case