Permalink
Browse files

Added init and destroy methods

  • Loading branch information...
nzakas
nzakas committed Feb 4, 2011
1 parent 635bd60 commit d46f0458bb7d166e5db8db8ad32cc331caf24a6c
View
@@ -1,4 +1,4 @@
-<!doctype html>
+<!DOCTYPE html>
<html>
<head>
<title>YUI Test Standalone Library</title>
@@ -123,6 +123,44 @@ <h4 id="setup-and-teardown">setUp() and tearDown()</h4>
to free up the memory. Real-world implementations will have more complex tests, of course, but they should follow the basic pattern you see in the above code.</p>
<p><strong>Note: </strong>Both <code>setUp()</code> and <code>tearDown()</code> are optional methods and are only used when defined.</p>
+ <h4 id="init-and-destroy">init() and destroy()</h4>
+ <p>Sometimes you may need to initialize some data before all test are run in a single test case. The <code>init()</code> method is run once before the tests start to run (also before the first call to <code>setUp()</code>. Likewise, the <code>destroy()</code> method is run just once after every test has been executed (and after the last <code>tearDown()</code> call). For example:</p>
+<pre><code>var testCase = new YUITest.TestCase({
+
+ name: "TestCase Name",
+
+ //---------------------------------------------
+ // init and destroy
+ //---------------------------------------------
+
+ init : function () {
+ this.data = { name : "Nicholas", age : 28 };
+ },
+
+ destroy : function () {
+ delete this.data;
+ },
+
+ //---------------------------------------------
+ // Tests
+ //---------------------------------------------
+
+ testName: function () {
+ YUITest.Assert.areEqual("Nicholas", this.data.name, "Name should be 'Nicholas'");
+ },
+
+ testAge: function () {
+ YUITest.Assert.areEqual(28, this.data.age, "Age should be 28");
+ }
+});
+
+</code></pre>
+ <p>In this example, a <code>init()</code> method creates a data object with some basic information. Each property of the data object is checked with
+ a different test, <code>testName()</code> tests the value of <code>data.name</code> while <code>testAge()</code> tests the value of <code>data.age</code>. Afterwards, the data object is deleted
+ to free up the memory using <code>destroy()</code>. Unlike the previous example using <code>setUp()</code> and <code>tearDown()</code>, both tests are running on the same object.
+ <p><strong>Note: </strong>Both <code>init()</code> and <code>destroy()</code> are optional methods and are only used when defined. You can use <code>init()</code> and <code>destroy()</code> together with <code>setUp()</code> and <code>tearDown()</code> for maximum amount of control over test data.</p>
+
+
<h4 id="ignoring">Ignoring Tests</h4>
<p>There may be times when you want to ignore a test (perhaps the test is invalid for your purposes or the functionality is being re-engineered and so it shouldn't be tested at this time). To specify tests to ignore,
use the <code>_should.ignore</code> property and name each test to skip as a property whose value is set to <code>true</code>:</p>
@@ -1901,12 +1901,30 @@ YUITest.TestCase.prototype = {
// Stub Methods
//-------------------------------------------------------------------------
+ /**
+ * Function to run once before tests start to run.
+ * This executes before the first call to setUp().
+ */
+ init: function(){
+ //noop
+ },
+
+ /**
+ * Function to run once after tests finish running.
+ * This executes after the last call to tearDown().
+ */
+ destroy: function(){
+ //noop
+ },
+
+
/**
* Function to run before each test is executed.
* @return {Void}
* @method setUp
*/
setUp : function () {
+ //noop
},
/**
@@ -1915,6 +1933,7 @@ YUITest.TestCase.prototype = {
* @method tearDown
*/
tearDown: function () {
+ //noop
}
};
@@ -2650,6 +2669,7 @@ YUITest.CoverageFormat = {
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_SUITE_COMPLETE_EVENT, testSuite: node.testObject, results: node.results});
} else if (node.testObject instanceof YUITest.TestCase){
+ node.testObject.destroy();
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_CASE_COMPLETE_EVENT, testCase: node.testObject, results: node.results});
}
@@ -2734,6 +2754,7 @@ YUITest.CoverageFormat = {
} else if (testObject instanceof YUITest.TestCase){
this.fire({ type: this.TEST_CASE_BEGIN_EVENT, testCase: testObject });
node._start = new Date();
+ testObject.init();
}
//some environments don't support setTimeout

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1901,12 +1901,30 @@ YUITest.TestCase.prototype = {
// Stub Methods
//-------------------------------------------------------------------------
+ /**
+ * Function to run once before tests start to run.
+ * This executes before the first call to setUp().
+ */
+ init: function(){
+ //noop
+ },
+
+ /**
+ * Function to run once after tests finish running.
+ * This executes after the last call to tearDown().
+ */
+ destroy: function(){
+ //noop
+ },
+
+
/**
* Function to run before each test is executed.
* @return {Void}
* @method setUp
*/
setUp : function () {
+ //noop
},
/**
@@ -1915,6 +1933,7 @@ YUITest.TestCase.prototype = {
* @method tearDown
*/
tearDown: function () {
+ //noop
}
};
@@ -2650,6 +2669,7 @@ YUITest.CoverageFormat = {
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_SUITE_COMPLETE_EVENT, testSuite: node.testObject, results: node.results});
} else if (node.testObject instanceof YUITest.TestCase){
+ node.testObject.destroy();
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_CASE_COMPLETE_EVENT, testCase: node.testObject, results: node.results});
}
@@ -2734,6 +2754,7 @@ YUITest.CoverageFormat = {
} else if (testObject instanceof YUITest.TestCase){
this.fire({ type: this.TEST_CASE_BEGIN_EVENT, testCase: testObject });
node._start = new Date();
+ testObject.init();
}
//some environments don't support setTimeout
@@ -1901,12 +1901,30 @@ YUITest.TestCase.prototype = {
// Stub Methods
//-------------------------------------------------------------------------
+ /**
+ * Function to run once before tests start to run.
+ * This executes before the first call to setUp().
+ */
+ init: function(){
+ //noop
+ },
+
+ /**
+ * Function to run once after tests finish running.
+ * This executes after the last call to tearDown().
+ */
+ destroy: function(){
+ //noop
+ },
+
+
/**
* Function to run before each test is executed.
* @return {Void}
* @method setUp
*/
setUp : function () {
+ //noop
},
/**
@@ -1915,6 +1933,7 @@ YUITest.TestCase.prototype = {
* @method tearDown
*/
tearDown: function () {
+ //noop
}
};
@@ -3805,6 +3824,7 @@ YUITest.PageManager = YUITest.Util.mix(new YUITest.EventTarget(), {
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_SUITE_COMPLETE_EVENT, testSuite: node.testObject, results: node.results});
} else if (node.testObject instanceof YUITest.TestCase){
+ node.testObject.destroy();
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_CASE_COMPLETE_EVENT, testCase: node.testObject, results: node.results});
}
@@ -3889,6 +3909,7 @@ YUITest.PageManager = YUITest.Util.mix(new YUITest.EventTarget(), {
} else if (testObject instanceof YUITest.TestCase){
this.fire({ type: this.TEST_CASE_BEGIN_EVENT, testCase: testObject });
node._start = new Date();
+ testObject.init();
}
//some environments don't support setTimeout
Oops, something went wrong.

0 comments on commit d46f045

Please sign in to comment.