My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
TestCase  
Everything you need to know about writing tests.
Updated Oct 13, 2010 by aeagle22206

Declaring a TestCase

It is necessary to tell the test runner about your tests suites. Most test runners deal with this by letting you build up test suites. This can be tedious and error prone resulting for un-run tests. In JsTestDriver we register the test case during the declaration phase. There are two ways to declare tests in the TestCase.

Using prototype:

MyTestCase = TestCase("MyTestCase");

MyTestCase.prototype.testA = function(){
};

MyTestCase.prototype.testB = function(){
};

Using inline declaration:

TestCase("MyTestCase", {
    testA:function(){
    },
    testB:function(){
    }
  });

Test life-cycle

When tests execute they follow JUnit life-cycle:

  1. Instantiate new instance of TestCase for each test method.
  2. Execute the setUp() method.
  3. Execute the testMethod() method.
  4. Execute the tearDown() method.

Asserts

The asserts are declared in Asserts.js. They follow JUnit assert conventions. This means that first argument is message and is optionally followed by expected and actual values.

expectAsserts(count)

This asserts tells the JsTestDriver how many asserts to expect. This is useful with callbacks.

MyTestCase.prototype.testExample = function () {
  expectAsserts(1);
  var worker = new Worker();
  var doSomething = {};
  worker.listener = function (work){
    assertSame(doSomething, work);
  };
  worker.perform(doSomething);
};

In the above example we have a callback function which contains an assert. However if the Worker does not call the callback function than the test would pass even thought the callback did not get called. To prevent this false positive the expectAsserts tells the test runner that the test is only valid if one assert has been called.

fail([msg])

assertTrue([msg], actual)

assertFalse([msg], actual)

assertEquals([msg], expected, actual)

assertSame([msg], expected, actual)

assertNotSame([msg], expected, actual)

assertNull([msg], actual)

assertNotNull([msg], actual)

Console

Some browsers provide console object which can be forwarded to the test runner output with -captureConsole flag. See CommandLineFlags. In addition there is jstestdriver.console class provided for you which is always forwarded to the test runners standard output.

ConsoleTest = TestCase("ConsoleTest");

ConsoleTest.prototype.testGreet = function() {
  jstestdriver.console.log("JsTestDriver", "Hello World!");
  console.log("Browser", "Hello World!");
};

The result of the above test when run is shown (the browser message will only be shown when -captureConsole is used.)

$ java -jar JsTestDriver.jar --tests all --captureConsole
.
Total 1 tests (Passed: 1; Fails: 0; Errors: 0) (1.00 ms)
  Safari 528.16: Run 1 tests (Passed: 1; Fails: 0; Errors 0) (1.00 ms)
    ConsoleTest.testGreet passed (1.00 ms)
      [LOG] JsTestDriver Hello World!
      [LOG] Browser Hello World!
Comment by seasprocket@gmail.com, Aug 6, 2010

This list of asserts is out-of-date. More complete list is documented in Assertions.

Comment by julian.b...@gmail.com, Oct 30, 2010

Would probably be worth mentioning that the inline declaration form requires tests to begin with "test".

Comment by machineg...@gmail.com, Dec 27, 2010

I'll second Julian's comment (and it seemed to have the same issue with the protoypal form for me as well).

Comment by dewilh...@gmail.com, Apr 21, 2011

jslint is not happy that you made a capitalized function which is not a constructor function. Any chance you can you provide an alternate lowercase version ?

Comment by dewilh...@gmail.com, Apr 21, 2011

I'm referring to the TestCase function, btw :)

Comment by dewilh...@gmail.com, Apr 21, 2011

/jslint newcap:false/

also works, if you're running a recent build of jslint.

Comment by Carlos.V...@gmail.com, Jun 16, 2011

Is there a way to specify a Testing Suite?

Comment by jakkie...@gmail.com, Apr 26, 2012

Is there any restrictions on the name of the test function.

When i write the following as testA() and testB(), both of them will be implemented.

AjaxCreateTest? = TestCase("AjaxCreateTest?"); AjaxCreateTest?.prototype.testA = function(){ }; AjaxCreateTest?.prototype.testB = function(){};

But if i change the name from "testB" to "AjaxCreateT", just testA() will be implemented. So wierd.

Comment by dharkness, May 24 (2 days ago)

Jakkie, I assume it works like JUnit where each test method must start with "test". JUnit now has annotations to mark tests, but that's not available in JS.


Sign in to add a comment
Powered by Google Project Hosting