Using Test Suites

nzakas edited this page Nov 23, 2011 · 1 revision

For large applications, you'll probably have many test cases that should be run during a testing phase. A test suite helps to handle multiple test cases by grouping them together into functional units that can be run together. To create new test suite, use the YUITest.TestSuite constructor and pass in the name of the test suite. The name you pass in is for logging purposes and allows you to discern which TestSuite instance currently running. For example:

//create the test suite
var suite = new YUITest.TestSuite("TestSuite Name");

//add test cases
suite.add(new YUITest.TestCase({
    //...
}));
suite.add(new YUITest.TestCase({
    //...
}));
suite.add(new YUITest.TestCase({
    //...
}));

Here, a test suite is created and three test cases are added to it using the add() method. The test suite now contains all of the information to run a series of tests.

It's also possible to add other multiple TestSuite instances together under a parent TestSuite using the same add() method:

//create a test suite
var suite = new YUITest.TestSuite("TestSuite Name");

//add a test case
suite.add(new YUITest.TestCase({
    //...
});

//create another suite
var anotherSuite = new YUITest.TestSuite("test_suite_name");

//add a test case
anotherSuite.add(new YUITest.TestCase({
    //...
});

//add the second suite to the first
suite.add(anotherSuite);

By grouping test suites together under a parent test suite you can more effectively manage testing of particular aspects of an application. Test suites may also have setUp() and tearDown() methods. A test suite's setUp() method is called before the first test in the first test case is executed (prior to the test case's setUp() method); a test suite's tearDown() method executes after all tests in all test cases/suites have been executed (after the last test case's tearDown() method). To specify these methods, pass an object literal into the YUITest.TestSuite constructor:

//create a test suite
var suite = new YUITest.TestSuite({
    name : "TestSuite Name",
    
    setUp : function () {
        //test-suite-level setup 
    },
    
    tearDown: function () {
        //test-suite-level teardown
    }
});

Test suite setUp() and tearDown() may be helpful in setting up global objects that are necessary for a multitude of tests and test cases.