After wasting too much time trying to figure out ASUnit and feeling sick about how bloated its codebase was I decided to roll my own. This is the result.
Note: I later realized I had been looking only at asunit2 when I should have been looking at the refactored and better looking asunit25. That said asunit25 still looks like Java and Actionscript is not Java. LeanUnit is a whole lot leaner and to the point if you’ll pardon my shameless self promotion.
A flash compiler. Example files are provided for Flash 8 and MTASC.
If you don’t have git you can get the files by using the download link at the github homepage. Otherwise just clone them from git.
git clone git://github.com/druiden/leanunit-actionscript2.git
Then either copy the leanUnit directory into your actionscript project source directory or link the cloned directory in your fla classpath (publish settings -> flash -> settings).
After installing, the first thing you need to do is create a test case class with the tests you want to run. If you don’t feel like starting from scratch just to see how it works you can go ahead and checkout the ‘example’ directory.
There are some conventions for how you name your test classes and where you put them.
-
They should reside in a ‘test’ directory relative to your projects source directory.
-
They should start with the name of the class you want to test.
-
They should end with Test.as.
So if you’re making a test class for your Person class you would create test/PersonTest.as. You are not forced to follow the conventions but I strongly recommend it to keep things nice and structured.
So now that your newly created test class looks good from the outside. What to make of the inside.
-
All test classes must inherit from leanUnit.TestCase
-
All test functions must start with ‘test’
-
You can add a setup function that will be run before every test function
-
You can add a teardown functino that will be run after every test function
The following is an example of a test class for String.
// test/StringTest.as class test.StringTest extends leanUnit.TestCase { var string:String
function setup() { string = 'Asdf' }
function testLength() { assertEqual(4, string.length) }
function testToUpperCase() { assertEqual( 'ASDF', string.toUpperCase() ) }
function teardown() { delete string } }
When your test class is created you can run it by adding it to a test suite call run on the suite instance.
Add code for running your test class in the framescript of the .fla, like:
import leanUnit.* import test.* new TestSuite( StringTest ).run()
Then export the .fla and watch the results.
Create an .as file to be the entry point for the compiler:
// MtascMain.as import leanUnit.* import test.* class MtascMain { static function main(root) { var suite = new TestSuite( StringTest ) suite.run() } }
Then compile from the command line using something like:
mtasc -swf test/example.swf -header 550:400:10:080808 -main MtascMain.as
Now you can run the generated .swf through Flash Player or from your webbrowser and watch the results.
You can add any number of test cases you want to run to your test suite before running it. TestSuite extends Array so you can work with it just like an array.
import leanUnit.* import test.* // The horisontal way var suite = new TestSuite( StringTest, ArrayTest ) suite.run() // The vertical way var suite = new TestSuite() suite.push( StringTest ) suite.push( ArrayTest ) suite.run() // The oldschool way var suite = new TestSuite() suite[0] = StringTest suite[1] = ArrayTest suite.run()
(The MIT License)
Copyright © 2008 David Backéus
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.