Search code examples
apache-flexunit-testingflexunit4

Flex Unit 4 -- Hello World


I want to use the Flex Unit 4 Suite.

I don't really have any experience with unit testing.

I downloaded the Turnkey project but I was a little overwhelmed.

I basically just want to start by creating a simple hello world unit test.

if I have a class called MyClass with 2 methods square() and cube().

and I want to create a unit test like so:

public class MyTest 
{
    public function testMyClass():void
    {
        var myClass:MyClass = new MyClass();

        assert(myClass.square(7) == 49);
        assert(myClass.cube(7) == 343);
        assert(myClass.square(5) == 50); // should fail
    }
}

How can I get this to work?


Solution

  • Add a new application to your Flex project -- name it with a suffix of 'UnitTest.mxml'. Add a reference to TestRunnerBase, and on creationComplete start the TestRunnerBase. This should get you started:

    <?xml version="1.0" encoding="utf-8"?>
    
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:flexunit="flexunit.flexui.*" creationComplete="init();"><mx:Script>
      <![CDATA[
    
            import flexunit.framework.TestSuite;
      import FlexUnit.*;
    
      private function init():void{
       test.test = initSuite();
       test.startTest();
      }
    
      private function initSuite():TestSuite{
       var suite:TestSuite = new TestSuite();
       suite.addTestSuite(testMyClass);
       return suite;
      }
      ]]>
     </mx:Script>
     <flexunit:TestRunnerBase id="test" width="100%" height="100%" />
    </mx:Application>