src/cm/media/js/lib/yui/yui_3.10.3/docs/test/test-async-test.html
author Yves-Marie Haussonne <ymh.work+github@gmail.com>
Fri, 09 May 2014 18:35:26 +0200
changeset 656 a84519031134
parent 525 89ef5ed3c48b
permissions -rw-r--r--
add link to "privacy policy" in the header test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Asynchronous Testing</title>
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
    <link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
    <script src="../../build/yui/yui-min.js"></script>
    
</head>
<body>
<!--
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
-->
<div id="doc">
    <div id="hd">
        <h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
    </div>
    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

            <h1>Example: Asynchronous Testing</h1>
    <div class="yui3-g">
        <div class="yui3-u-3-4">
            <div id="main">
                <div class="content"><div class="intro">
    <p>This example shows how to create an asynchronous test with the YUI Test framework for testing browser-based JavaScript code.
      A <code>Y.Test.Case</code> object is created with a test that waits for a
      few seconds before continuing. The <code>Y.Test.Runner</code>
      is then used to run the tests once the page has loaded.</p>
</div>

<div class="example yui3-skin-sam">
    <div id="testLogger"></div>

<script>
YUI().use('node', 'test-console', 'test',function (Y) {

    Y.namespace("example.test");

    Y.example.test.AsyncTestCase = new Y.Test.Case({

        //name of the test case - if not provided, one is auto-generated
        name : "Asynchronous Tests",

        //---------------------------------------------------------------------
        // setUp and tearDown methods - optional
        //---------------------------------------------------------------------

        /*
         * Sets up data that is needed by each test.
         */
        setUp : function () {
            this.data = {
                name: "test",
                year: 2007,
                beta: true
            };
        },

        /*
         * Cleans up everything that was created by setUp().
         */
        tearDown : function () {
            delete this.data;
        },

        //---------------------------------------------------------------------
        // Test methods - names must begin with "test"
        //---------------------------------------------------------------------

        testWait : function (){
            var Assert = Y.Assert;

            //do some assertions now
            Assert.isTrue(this.data.beta);
            Assert.isNumber(this.data.year);

            //wait five seconds and do some more
            this.wait(function(){

                Assert.isString(this.data.name);

            }, 5000);

        }

    });

    //create the console
    (new Y.Test.Console({
        newestOnTop : false,
        filters: {
            pass: true,
            fail: true
        }
    })).render('#testLogger');

    Y.Test.Runner.add(Y.example.test.AsyncTestCase);

    //run the tests
    Y.Test.Runner.run();
});

</script>

</div>

<h2 class="first" id="asynchronous-test-example">Asynchronous Test Example</h2>

<p>This example begins by creating a namespace:</p>
<pre class="code prettyprint">Y.namespace(&quot;example.test&quot;);</pre>

<p>This namespace serves as the core object upon which others will be added (to prevent creating global objects).</p>

<h3 id="creating-the-testcase">Creating the TestCase</h3>

<p>The first step is to create a new <code>Y.Test.Case</code> object called <code>AsyncTestCase</code>.
  To do so, using the <code>Y.Test.Case</code> constructor and pass in an object literal containing information about the tests to be run:</p>
<pre class="code prettyprint">Y.example.test.AsyncTestCase = new Y.Test.Case({

    &#x2F;&#x2F;name of the test case - if not provided, one is auto-generated
    name : &quot;Asynchronous Tests&quot;,

    &#x2F;&#x2F;---------------------------------------------------------------------
    &#x2F;&#x2F; setUp and tearDown methods - optional
    &#x2F;&#x2F;---------------------------------------------------------------------

    &#x2F;*
     * Sets up data that is needed by each test.
     *&#x2F;
    setUp : function () {
        this.data = {
            name: &quot;test&quot;,
            year: 2007,
            beta: true
        };
    },

    &#x2F;*
     * Cleans up everything that was created by setUp().
     *&#x2F;
    tearDown : function () {
        delete this.data;
    },

    &#x2F;&#x2F;---------------------------------------------------------------------
    &#x2F;&#x2F; Test methods - names must begin with &quot;test&quot;
    &#x2F;&#x2F;---------------------------------------------------------------------

    testWait : function (){
        var Assert = Y.Assert;

        &#x2F;&#x2F;do some assertions now
        Assert.isTrue(this.data.beta);
        Assert.isNumber(this.data.year);

        &#x2F;&#x2F;wait five seconds and do some more
        this.wait(function(){

            Assert.isString(this.data.name);

        }, 5000);

    }

});</pre>

<p>The object literal passed into the constructor contains two different sections. The first section contains the <code>name</code> property,
  which is used to determine which <code>Y.Test.Case</code> is being executed. A name is necessary, so one is generated if it isn't specified.</p>
<p>Next, the <code>setUp()</code> and <code>tearDown()</code> methods are included. The <code>setUp()</code> method is used in a <code>Y.Test.Case</code>
  to set up data that may be needed for tests to be completed. This method is called immediately before each test is executed. For this example,
  <code>setUp()</code> creates a data object. The <code>tearDown()</code> is responsible for undoing what was done in <code>setUp()</code>. It is
  run immediately after each test is run and, in this case, deletes the data object that was created by <code>setUp</code>. These methods are optional.</p>
<p>The second section contains the actual tests to be run. The only test is <code>testWait()</code>, which demonstrates using
  the <code>wait()</code> method to delay test execution. There are two arguments passed in: a function to run once the test resumes
  and the number of milliseconds to wait before running this function (same basic format as <code>setTimeout()</code>). When
  the test resumes, the function is executed in the context of the <code>Y.Test.Case</code> object, meaning that it still has
  access to all of the same data as the test that called <code>wait()</code>, including properties and methods on the <code>Y.Test.Case</code>
  itself. This example shows the anonymous function using both the <code>Y.Assert</code> object and the <code>data</code> property
  of the <code>Y.Test.Case</code>.</p>

<h3 id="running-the-tests">Running the tests</h3>

<p>With all of the tests defined, the last step is to run them:</p>

<pre class="code prettyprint">&#x2F;&#x2F;create the console
(new Y.Test.Console({
    verbose : true,
    newestOnTop : false
})).render(&#x27;#testLogger&#x27;);

&#x2F;&#x2F;add the test suite to the runner&#x27;s queue
Y.Test.Runner.add(Y.example.test.AsyncTestCase);

&#x2F;&#x2F;run the tests
Y.Test.Runner.run();</pre>


<p>Before running the tests, it's necessary to create a <code>Y.Test.Console</code> object to display the results (otherwise the tests would run
  but you wouldn't see the results). After that, the <code>Y.Test.Runner</code> is loaded with the <code>Y.Test.Case</code> object by calling
  <code>add()</code> (any number of <code>Y.Test.Case</code> and <code>TestSuite</code> objects can be added to a <code>TestRunner</code>,
  this example only adds one for simplicity). The very last step is to call <code>run()</code>, which begins executing the tests in its
  queue and displays the results in the <code>Y.Test.Console</code>.</p>

<h2 id="complete-example-source">Complete Example Source</h2>

<pre class="code prettyprint">&lt;div id=&quot;testLogger&quot;&gt;&lt;&#x2F;div&gt;

&lt;script&gt;
YUI().use(&#x27;node&#x27;, &#x27;test-console&#x27;, &#x27;test&#x27;,function (Y) {

    Y.namespace(&quot;example.test&quot;);

    Y.example.test.AsyncTestCase = new Y.Test.Case({

        &#x2F;&#x2F;name of the test case - if not provided, one is auto-generated
        name : &quot;Asynchronous Tests&quot;,

        &#x2F;&#x2F;---------------------------------------------------------------------
        &#x2F;&#x2F; setUp and tearDown methods - optional
        &#x2F;&#x2F;---------------------------------------------------------------------

        &#x2F;*
         * Sets up data that is needed by each test.
         *&#x2F;
        setUp : function () {
            this.data = {
                name: &quot;test&quot;,
                year: 2007,
                beta: true
            };
        },

        &#x2F;*
         * Cleans up everything that was created by setUp().
         *&#x2F;
        tearDown : function () {
            delete this.data;
        },

        &#x2F;&#x2F;---------------------------------------------------------------------
        &#x2F;&#x2F; Test methods - names must begin with &quot;test&quot;
        &#x2F;&#x2F;---------------------------------------------------------------------

        testWait : function (){
            var Assert = Y.Assert;

            &#x2F;&#x2F;do some assertions now
            Assert.isTrue(this.data.beta);
            Assert.isNumber(this.data.year);

            &#x2F;&#x2F;wait five seconds and do some more
            this.wait(function(){

                Assert.isString(this.data.name);

            }, 5000);

        }

    });

    &#x2F;&#x2F;create the console
    (new Y.Test.Console({
        newestOnTop : false,
        filters: {
            pass: true,
            fail: true
        }
    })).render(&#x27;#testLogger&#x27;);

    Y.Test.Runner.add(Y.example.test.AsyncTestCase);

    &#x2F;&#x2F;run the tests
    Y.Test.Runner.run();
});

&lt;&#x2F;script&gt;</pre>

</div>
            </div>
        </div>

        <div class="yui3-u-1-4">
            <div class="sidebar">
                
                    <div id="toc" class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Table of Contents</h2>
                        </div>

                        <div class="bd">
                            <ul class="toc">
<li>
<a href="#asynchronous-test-example">Asynchronous Test Example</a>
<ul class="toc">
<li>
<a href="#creating-the-testcase">Creating the TestCase</a>
</li>
<li>
<a href="#running-the-tests">Running the tests</a>
</li>
</ul>
</li>
<li>
<a href="#complete-example-source">Complete Example Source</a>
</li>
</ul>
                        </div>
                    </div>
                

                
                    <div class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Examples</h2>
                        </div>

                        <div class="bd">
                            <ul class="examples">
                                
                                    
                                        <li data-description="Demonstrates basic usage of YUI Test for setting up and running tests.">
                                            <a href="test-simple-example.html">Simple Testing Example</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Demonstrates how to use advanced testing features such as defining tests that should fail, tests that should be ignored, and tests that should throw an error.">
                                            <a href="test-advanced-test-options.html">Advanced Test Options</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Demonstrates how to use the ArrayAssert object to test array data.">
                                            <a href="test-array-tests.html">Array Processing</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Demonstrates basic asynchronous tests.">
                                            <a href="test-async-test.html">Asynchronous Testing</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Demonstrates using events with asynchronous tests.">
                                            <a href="test-async-event-tests.html">Asynchronous Event Testing</a>
                                        </li>
                                    
                                
                            </ul>
                        </div>
                    </div>
                

                
            </div>
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

<script>
YUI.Env.Tests = {
    examples: [],
    project: '../assets',
    assets: '../assets/test',
    name: 'test-async-test',
    title: 'Asynchronous Testing',
    newWindow: '',
    auto:  false 
};
YUI.Env.Tests.examples.push('test-simple-example');
YUI.Env.Tests.examples.push('test-advanced-test-options');
YUI.Env.Tests.examples.push('test-array-tests');
YUI.Env.Tests.examples.push('test-async-test');
YUI.Env.Tests.examples.push('test-async-event-tests');

</script>
<script src="../assets/yui/test-runner.js"></script>



</body>
</html>