Home  >  

Flash TDD Project Template

Author photo
November 30, 2009 | | Comments (5)
AddThis Social Bookmark Button
aft_logo.jpg

Advanced Flash Tactics or AFTs are techniques that come from deep within the Flash Art Of War, the oldest Flash military treatise in the world. In this AFT I will go over - Flash TDD Project Template. Those of you who know me or follow me on twitter may have a good idea of how much I love ANT for automating my compiles. As I started working on implementing Flash TDD into my workflow it became very clear I needed a way to automate my tests as well. Enter the Flash TDD Project Template.

github_project

Before we go into how my project works lets look at the problem I had. One of the goals of TDD is to run your tests before you compile. If the test fails, you shouldn't be able to build the project. This lets you know right away that your build is bad. As of now there is no way to automate this in Flash Builder. The process of running tests requires you to click the test's run and then the click the compiler run. Of course you can bind these commands to keyboard shortcuts but I am lazy and don't like doing more then I have to. Using ANT to automate this process is easy and reusable from project to project. Here is the outline of what I wanted to achieve with my Flash TDD Project Template:

  • Clean/Remove files generated in the previous build.
  • Run my unit test suite.
  • If any test fails, build a report and open it.
  • If the test pass, compile.

For good measure I added these additional steps:

  • Generate ASDocs before doing a compile.
  • Include targets for creating a SWC or a SWF.
  • Automate setting up my bin folder and html/external resources files.
  • Remove git specific files when setting up for the first time.

At this point I really want to thank the Robot Legs team for helping me indirectly create this ANT build. I modified their build file and project organization so without that as a reference this project would have taken me a lot longer to create. I have altered the build file a lot from what they originally used but I suggest you check out the framework and how they are automating their build process.

Now onto how this project works. I want to go over how to set up the project and point out a few configuration tips. Feel free to check out the build file itself and how I structured everything. In a later post I may go over the inner workings of using ANT and why this project template is set up the way it is. For now, if you follow these steps you should be up and running in no time.

It is important to note that you should have ANT installed on your computer or in Flash Builder. If you don't, check out this link: How to install ANT in Flash Builder 4. If you installed Flash Builder as a plugin chances are you will already have ANT support installed in Eclipse.

No Command Line Setup

  1. Create a new project in FlashBuilder or use an existing one.
  2. Goto the GitHub project page here.
  3. Click on the download button. Here you can choose to download a tagged version or the main trunk.
  4. Once it is downloaded, copy the files over to your project or import them inside of Eclipse.
  5. You should now be ready to configure it, see below "Setting Up The Build Properties".

Command Line Setup

  1. Create a new project in FlashBuilder or use an existing one.
  2. CD into your project folder.
  3. You will need to setup git by typing ~git init
  4. Now add the GitHub project path by typing ~git remote add origin git@github.com:theflashbum/FlashTDDProject
  5. Next you will need to pull down all the files ~git pull origin master
  6. Finally if you have ANT accecible on the command line you can have it remove the git project file by typing in ~ant disconnect



commandline_setup

Setting Up The Build Properties

At this point you should everything you need to get started, you just need to configure a few variables.

  1. Copy the build.template.properties files and rename it to build.properties.
  2. Open it up and change the path to the FLEX_HOME to where you keep your Flex SDK on your computer. **Don't change the project name just yet**

Run The ANT Build

Now that you have the FLEX_HOME path setup you should be able to run the project. Go into Flash Builder and right click on the build file. Select Run As -> ANT. If you do not see ANT as an option you probably don't have it installed.

On your first run you should notice that the Test Report pops up saying you have 1 error. That is because the project is automatically set up to fail on initial setup. Once you start building and passing your tests you will not see the report and instead the final html document with your swf will load up.

Where Things Goes

If you have worked in Flash Builder before, the project setup should be familiar. I simply moved a few things around to clean up the root of the project.

project
  • src - This is where all of your source code goes.
  • test - This is where you will put your test classes.
  • build/assets - This is for assets, fonts or other files you would embed into your code.
  • build/bin-resources - This is where all your html support files would go such as JS to embed your swf, any xml/css that is loaded in at run time and even images. Think of this as the folder of files you would want to live on your server when you are ready to deploy.
  • build/html-template - This is your HTML Template, just like you had auto generated for you by Flash Builder. You can modify this file if you want to add anything to the HTML template. It has it's own property file you can configure or just stick your own template in here.
  • build/libs - This is where any SWCs or JARs would go that are required by the project to run. All dependent AS 3 code libraries should go here, so make sure you add build/libs to your library path.

Make sure you uncheck Copy non-embedded files and HTML wrapper in the Project -> Properties -> ActionScript Compiler window. You will not be using the built in run anyway but this is just good practice when setting up a project to run with ANT in Flash Builder.

Where To Go From Here

Now that we have gone over the basic setup you should go back into the build.properties file to rename the project and give it a version number. Once you do this make sure you rename your test suite (test/FlashTDDProjectTestApp + FlashTDDProjectTestSuite.as) and the doc class (src/FlashTDDProject.as). The only other part I didn't cover is how to change between a Flash Site or a SWC. Simply open up the build.xml file and change the default attribut at the top of the document to compile-swc. Likewise if you just want to build a swf and not deal with html files you can set the default to compile-swf. Check the usage target for all the commands the build script can execute.

Hope you find this project template as helpful as I do. I am still cleaning this up and adding new features so consider it an early preview. If you have any questions or problems getting this to run please leave a comment below or fill out an issue here.

Read more from Jesse Freeman. Jesse Freeman's Atom feed TheFlashBum on Twitter

Comments

5 Comments

Alan said:

Been following your and Rob's TDD thoughts on twitter, and now that you've been playing with TDD more and more, how do you feel it fits in with Flash ( as a technology ).

For frameworks like your FCSS, I can see the benefit, but what about most Flash work?

I'll elaborate, I watched a great discussion between Uncle Bob and another notable C developer - they were discussing whether TDD was a good methodology. During the course of the discussion, Bob decides to use a case example. It starts off with...

'Ok lets assume a project with.....2 million lines of code...'

'oh so a small project'

'yes...'

Most Flash work dwarfs to this comparison. With the methodology of TDD geared towards projects much, much larger than most any Flash developer will encounter. How much of a benefit do you think TDD lends to the Flash world?

Anonymous said:

@Alan - TDD isn't geared towards large applications only. TDD encourages simple designs that can be used to build large applications but it is definitely a viable strategy in smaller-scale apps as well. The benefits (and ROI) of TDD techniques can definitely be found in smaller applications.

Jesse Freeman said:

Using TDD in Flash development is a double edged sword. I really see a value using it on data driven sites, frameworks, and low level testing but with visual heavy sites it's useless. A large hurdle for most Flash developers is knowing when and where to use TDD. This is something I am still trying to understand and it will only come with more time and experience. After 2 months of intensive trail, error and practicing I would be hard pressed to build a project/site without some form of TDD but again it really goes back to what you are building and what you hope to achieve with testing.

Not to scare anyone off but once you get my Flash TDD Project up and running and do a compile you will notice it takes a long time before you even get to see the final product. If a test fails you have to deal with the failure first. This goes against the mentality most of us have in Flash; we have been use to immediate gratification for a very long time. I do have this to say, if it wasn't for TDD I wouldn't have been able to get as far as I have with my FCSS project. I have discovered more bugs and helped add even more complex features that would have been impossible to verify worked for everyone who touches the library. Now I know 100% of the time exactly how things should work and can take a large part of the guess work out of my coding.

TDD may not be for everyone but there are practical lessons to be learned by writing a test first (thinking about the code you want to write and what it will do), adding the code that is being tested (implement the code you just planed out), and verifying it passes the test (validating that your code works 100% of the time in multiple use cases).

equ said:

You may also look at the Antennae project, which helps with Flex and Ant integration:

http://code.google.com/p/antennae/

karfau said:

Finally had some time to try it out.

So some tips for windows users:
in the target junitreport I changed the exec to the following

<exec executable="${browser}" spawn="true">
    <arg line="--app=file:///${report.url}"/>
</exec>

and set the browser property in build.properties to the path of my favorite "browser for debugging" (it is not pure chrome). As for the path to FLEX_HOME, it needs \\ instead of \ in the path.
(spawn is opening the browser and doesn't wait until it gets closed to continue the build.)

But my real questions are:
1.) Is there any way / what part is missing so that I can use this with asunit?

2.) Is there a way to connect it with the FlexUnit-Panel/View in FlashBuilder?

Anyway, thx for the great work.
(I will fork and push the changes for win-os as soon as I had some more time to play with it.)

karfau

Leave a comment


Tag Cloud

iPad

What's your take on the iPad? (Putting aside the Flash/iPad flame war)

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.