views:

31

answers:

2

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?

+1  A: 

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>
adamcodes
A: 

So the problem is that your tests aren't even running? At some point, somewhere, you should see the display of your test results, whether they pass or fail.

I personally use ASUnit. When I was in a Flex project the other day - which was the first time i'd ever used ASUnit in a Flex proj - when I pressed the compile button, I was asked if I wanted to start up my application or if I wanted to start up the ASUnit test runner instead.

So yea, your tests have to be manually invoked somehow, like adamcodes suggested.

Montagist