Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Context Data

nzakas edited this page · 1 revision

Managing data amongst test suites and test cases can be difficult, which is why YUI Test has a concept called context data. When the test runner starts to execute, it creates an empty object. The object is passed in as an argument to every init(), destroy(), setUp(), tearDown(), and every test function. Because the same object is passed into each method (it "visits" each method), you have the opportunity to add, remove, or change data on the object that other tests might need. This is an advanced piece of functionality that may not be necessary for every group of tests, but allows powerful data sharing where necessary. Example:

var testCase = new YUITest.TestCase({

    name: "TestCase Name",

    init: function(data){
        data.foo = "bar";
    },

    testValueOfFoo : function (data) {
        YUITest.Assert.areEqual("bar", data.foo);
    }
});

Context data persists even once a test case is completed, so it can be passed from test suites to test cases and persist to other test cases. For example:

var testSuite = new YUITest.TestSuite({
    name: "Test Suite Name",

    setUp: function(data){
        data.topLevel = 1;
    }
});

testSuite.add(new YUITest.TestCase({

    name: "First Test Case",

    init: function(data){
        data.foo = "bar";
    },

    testValueOfFoo : function (data) {
        YUITest.Assert.areEqual("bar", data.foo);   //from init
    },

    testValueOfTopLevel: function(data){
        YUITest.Assert.areEqual(1, data.topLevel);  //from test suite
    }
});

testSuite.add(new YUITest.TestCase({

    name: "Second Test Case",

    testValueOfFoo : function (data) {
        YUITest.Assert.areEqual("bar", data.foo);   //from init in First Test Case
    },

    testValueOfTopLevel: function(data){
        YUITest.Assert.areEqual(1, data.topLevel);  //from test suite
    }
});
Something went wrong with that request. Please try again.