Skip to main content
December 21, 2009
Answered

How to create a TestRunner for FlexUnit 4?

  • December 21, 2009
  • 1 reply
  • 2488 views

I just started with FlexUnit 4 today and can already see the benefit of all of the new features.  I am a bit stumped on how to get the initial setup to work.  As far as I can tell, most of the tutorials, walkthroughs, installation instructions linked to from the Adobe site are for earlier versions of FlexBuilder, or they reference classes that don't exist in the version I just downloaded.  Can someone help me access these new features?

I created a test case that does not extend any base classes and uses hamcrest matchers.  Here is an excerpt:

public class RangeHashMapTest

{

  private var map:RangeHashMap;


  [Before]

  public function setup():void {

    map = new RangeHashMap(0, 50);

    map.put(6, "a");

  }


  [Test]

  public function testPut_Valid():void {

    map.put(5, "foo");

    assertThat("foo", equalTo(map.getValue(5)));

  }


  [Test(expects="TypeError")]

  public function testPut_invalid():void {

      map.put("a", "foo");

  }


  [Test]

  public function testGet():void {

    assertThat(map.getValue(-10), isA(nullValue()));

  }

}

I then created a TestRunner based on the FlexUnit walkthrough link on the Developer Documentation section of the FlexUnit 4 site:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"

        xmlns:flexunit="flexunit.flexui.*"

        creationComplete="onCreationComplete()">

 

  <mx:Script>

    <![CDATA[

      import com.ericfeminella.collections.RangeHashMapTest;

      import flexunit.framework.TestSuite;

     

      // Create the test suite and run the tests

      private function onCreationComplete():void

      {

          testRunner.test = createSuite();

          testRunner.startTest();

        }

     

      // Creates the test suite to run

      private function createSuite():TestSuite {

          var testSuite:TestSuite = new TestSuite();

         

          testSuite.addTestSuite( RangeHashMapTest );

         

          return testSuite;

        } 

    ]]>

  </mx:Script>


  <!-- FlexUnit GUI Component -->

  <flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" />

</mx:Application>

When I run this, I get an Error:

"Type Coercion failed: cannot convert ...:RangeHashMapTest@4c6e661 to flexunit.framework.Test."

I tried to have my test extend TestCase, and it ran successfully, but all of the tests fail, because the [Before] never gets executed, so map is always null.  It also says that all of my tests have no asserts.  It seems as though I am using the new code, but not able to take advantage of the new features.

Thanks!

Brian

This topic has been closed for replies.
Correct answer mlabriola

Okay, if you downloaded the flexunit_.9.zip then you are using a version several years out of date.

Read through the second link I sent you, but get all of your files from www.flexunit.org as that should ensure you have the latest.

Cheers,

Mike

1 reply

Participating Frequently
December 21, 2009

Brian,

The beginning of your info is all FlexUnit 4 stuff and in good shape. However, all of the parts you found about running a test pertain to FlexUnit1.That's where your problem comes in.

Check out these links and see if they help:

http://docs.flexunit.org/index.php?title=Test_Runner

and

http://www.insideria.com/2009/05/flashbuilder4-will-support-fle.html

Mike

December 21, 2009

Hi Mike,

Thanks for the quick reply.

I had come across an approach similar to the one in the first article you sent me looking at how the Hamcrest tests were written actually.  But I just can't get it to work in my environment. 

I am using Flex Builder 3.0, with the Flex SDK 3.4.  I went to check what version of FlexUnit I downloaded but it seems as though the Adobe Page is down :/... It was the latest one I believe (the one on top).  The zip I have is called flexunit0_9.zip.

Anyway, when I follow the instructions on this page (http://docs.flexunit.org/index.php?title=Test_Runner), I have a problem because I do not have a FlexUnitCore class.

Thanks!

mlabriolaCorrect answer
Participating Frequently
December 21, 2009

Okay, if you downloaded the flexunit_.9.zip then you are using a version several years out of date.

Read through the second link I sent you, but get all of your files from www.flexunit.org as that should ensure you have the latest.

Cheers,

Mike