Title: Unit testing in actionscript3 Date: 2011-02-15 19:14 Author: eamonnfaherty Category: testing Tags: Actionscript, as3, flashtdd, tdd Slug: unit-testing-in-actionscript3
I was asked by a friend recently to point him in the direction of some advice on unit testing within actionscript 3. I thought it may be useful to others so here is the email with some addition descriptions:
What is unit testing
Unit tests are methods that run in order to verify the output of a method is what is expected when given a predefined input. This is useful as you can check if your code does what it should. There is a whole world of information on this on the internet. Check out test driven development.
There used to be a few different unit testing frameworks - flexunit, fluint and asunit. asunit was the fastest to run but has not been too popular. I am not sure why. Fluint was a flex-centric testing framework with stuff like ui impersonator (mocking). Flexunit was the most used.
Flexunit was taken on (again) by Adobe and merged with fluint. After adobe took it on again they named it version 4, probably to bring versioning inline with Flash Builder and flex. with this change they improved the API to introduce metadata for methods so that they are run before, after and as test methods. It is now distributed with hamcrest (same as the java hamcrest library) and a number of runners and listeners. It is the most used and it is the one i would recommend.
How it works
You write the unit test in any of the supported styles (flexunit 1, flexunit 4 or fluent or maybe even in another style) and you mark them to run with the associated runner (using metadata). You then run the tests using that test runner. Whilst the test runs a listener is also run. There are multiple test listeners, they are for different environments - to be run on a ci box, in the browser, in the IDE etc.
You cannot currently run unit tests without a flash player. You cannot currently run a flash player without a virtual frame buffer (think that is the term) or without a window manager running. With hudson (or jenkins now) you can install and run the vnc plugin, this has been the easiest way I have found so far. This takes care of all that stuff for you. It even deals with multiple vnc sessions at the same time if you have more than one hudson executor at a time.
Coverage and static analysis
You can get coverage from running flexcover and there is a flex pmd library released by adobe - there is the usual pmd offering and a cpd plugin.
How to run it all
you can run it all using maven (using the flexmojo stuff) or you can roll out your own ant stuff. Rolling out your own ant stuff is the way to go if you have a complicated dependencies system (like at my last job), otherwise if you just want to get the job done maven is probably the way to go.
I will attach a working example project tomorrow.