FlexUnit 4 – Starting out


For any of you familiar with FlexUnit 1, Fluint, or FlexUnit 4, move along. This is probably going to be so simple you will scoff at it. For those of you who haven’t started out with any unit testing, especially the new FlexUnit 4, then read on.

First:

FlexUnit 4 has NOTHING to do with Flash Builder 4 in terms of version number. It actually relates to JUnit 4 (which specific version, I don’t know, someone?). So for those of you wondering, yes, it can test Flex 3 SDK , not just Flex 4 SDK projects.

Next: Getting started

Whether you have an already created project, or are starting a brand new one, there is a simple set of steps that you can perform in Flex Builder 4 to set up your project for unit testing.

I am writing this, because I thought it should be easy, and it is, I just went about it the wrong way and I want you out there to not have to make the same mistake.

Originally, I was right clicking on the Package Explorer and trying to use Execute FlexUnit Tests menu item. While this will eventually work, it won’t yet. Why? You haven’t set up your project to know that it has FlexUnit tests!

Steps: (This is the way I know how to do it, if you know another way, let me know!)

  1. Select Run > Run > FlexUnit Tests
    1. This brings up a progress dialog that sets up all of the libraries into your project.
    2. Then the Run FlexUnit Tests dialog comes up.
    3. If you haven’t written any tests yet, then you can only Cancel from this.
  2. Write some tests!
    1. Create a new package that will contain all of your tests. This way you can keep track of them more easily and separate them out of your production code.
    2. A simple testing class will look like this:
    3. package TestingPackage
      {
      	import flexunit.framework.Assert;
      
      	public class TestSampleLayout
      	{
      		[Test]
      		public function testAssertFalse():void
      		{
      			Assert.assertFalse( false );
      		}
      
      		[Test]
      		public function testSampleLayoutFunctionReturn():void
      		{
      			var initialString : String = "Hello World.";
      			var finalString : String;
      			finalString = new SampleLayout().sampleFunctionReturnTest( initialString );
      			Assert.assertEquals( initialString, finalString );
      		}
      
      		public function TestSampleLayout()
      		{
      		}
      	}
      }
    4. This class does two tests.
      1. It asserts that False really is false. You normally wouldn’t do this, but it shows you something of what you can do.
      2. It tests the method “sampleFunctionReturnTest” of the class SampleLayout. The method merely spits back whatever string it was entered.
    5. So when it gets run through AssertEquals, and they are equal, then the test passes. If they are not, then it would fail.
  3. Note that before each test method is [Test], this is what FlexUnit 4 uses to determine if a method is a test or not. So you need that before every test you want run.

I am still working out all of the different types of testing that are available, but this should hopefully get you started until I post more, or more documentation becomes available.

,

Leave a Reply