Jsunit

JSUnit has a number of capabilities, including automating test runs across machines.

JsUnit also provides a test server (Java) which allows you to automatically run tests in all available browsers issuing a single command.

Unit tests in JsUnit are called Test Functions.

Test Functions live in an HTML page called Test Page.

A Test Page is any HTML page that include jsUnitCore.js

jsUnitCore.js provides the assertion functions of JsUnit, e.g. assertEquals.

JsUnit supports setUp() and tearDown().

A Test Suite Page declares a suite() function that returns a JsUnitTestSuite for grouping Test Pages.

The JsUnit testRunner.html page runs Test Pages.

The TestRunner HTML page can be run from a file server or a web server.

JUnit JsUnit
Test Methods Test Functions
Test Classes Test Pages
Extend junit.framework.TestSuite Test Suite Pages (HTML)
AWT/Swing/IDE TestRunners Pure HTML/JS TestRunner
public void setUp(), tearDown() function setUp(), tearDown()
Runs in the target Java VM Runs in the target browser(s)
Pure Java Pure Javascript

JsUnit includes certain functions that output “traces” to a debugging window.

Tracing is an alternative to a debugger if one is not available for a certain browser/platform.

There are 3 trace functions: debug(message) (level 3), warn(message) (level 2), inform(message) (level 1)

For a particular run of the tests, you can set the trace level to any of the 3 levels.

JsUnit is not usually appropriate for submitting forms/other navigation that interacts with a web server. It is intended to test purely client-side functionality – it’s a unit testing framework of the in-browser JavaScript that usually lives in .js files included by generated/static HTML pages. Testing that “walk through” a web application should be done in HTTPUnit/JWebUnit. In most cases, the desire to submit a form points at the need to instead mock out the submission in order to create a true unit test.

There is currently work in progress to support the ability for a Test Page to submit a form in a separate page that lives in a Frame/IFrame in the Test Page.

Part 2. The automation of runs of JsUnit Test Pages/Suites on multiple local or remote browsers/OSs from an Ant script or JUnit suite (and hence in a continuous build).

Automation on a single machine: StandaloneTest.

JsUnit provides a Java JUnit TestCase class called net.jsunit.StandaloneTest

StandaloneTest can be invoked from the JsUnit Ant build.xml

You can configure StandaloneTest with respect to

  1. Which browsers to run tests in (a list of paths to browser executables)
  2. Which Test Page to run (usually it is your highest-level Test Suite Page)

When run, StandaloneTest starts each browser in turn and runs the specified Test Page without any user interaction.

If any test fails in any of the browsers, Standalone Test fails with a failure message indicating which test failed in which browser.

StandaloneTest: how it works

StandaloneTest starts a lightweight Java HTTP server called the JsUnitServer.

Each browser is launched as an external process.

Certain parameters are passed in the TestRunner URL that tell the test runner to collect results in a form field and submit the form to the JsUnitServer over HTTP.

The JsUnitServer waits for all browsers to finish, and inspects the submitted results.

If any browser fails, StandaloneTest fails.

Automation on multiple machines: DistributedTest

StandaloneTest allows for automated runs on a single machine, but we still need to be able to automatically launch all our tests on all target OSs.

This means we need to be able to programmatically kick off JsUnit tests on remote machines.

JsUnit provides a second Java JUnit TestCase class called net.jsunit.DistributedTest.

DistributedTest is configured to be aware of a list of multiple remote machines.

When run, DistributedTest asks each remote machine in turn to run specified Tests on the browsers relevant for the respective OS.

Each remote machine runs the specified tests on each browser relevant to its OS.

If any test fails on any browser on any remote machine, DistributedTest fails with an error message indicating the failure.

DistributedTest: how it works

We start a JsUnit server on each remote server.

Each JsUnit server is configured with target browsers for its OS.

The JsUnit servers listen for requests from DistributedTest to run tests.

DistributedTest tells each remote JsUnit server (over HTTP) to run tests.

Each JsUnitServer runs StandaloneTest.

For each JsUnitServer, if any test fails on any browser, a failure message is sent back to DistributedTest; otherwise a success message is returned (the messages are XML strings).

If any failure message is received, DistributedTest fails.

Running in a continuous build.

StandaloneTest can be repeatedly run by a developer as he/she is writing code and tests.

However, StandaloneTest is a JUnit test, so it can be added to a JUnit suite just like any other test, and thereby run in a continuous build.

Alternatively, because JsUnit can be launched from Ant, the Ant task can be added to a continuous build (such as Anthill or Cruise Control).

StandaloneTest: Logging.

For each run, StandaloneTest writes a log file to disk.

The filename is generated (and unique) or it can be specified by a parameter in the TestRunner.

The log file format matches JUnit’s; the details of each Test Function are given.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License