How to Use the ASUnit Framework to Write Unit Tests for ActionScript
2010-04-22 12:49 宝宝合凤凰 阅读(197) 评论(0) 收藏 举报
1) Create a directory called AsUnitExample
Download the asunit framework zip file from here and copy the contents of the as3 directory into your AsUnitExample directory. All of the files we create in the following example will be in the root directory AsUnitExample. At this point the AsUnitExample directory should contain the file AsUnitTestRunner.as and the directories asunit and mx.
2) Open and inspect the file AsUnitExample/AsUnitTestRunner.as Copy the following source code:
- package {
- import asunit.textui.TestRunner;
- public class AsUnitTestRunner extends TestRunner {
- public function AsUnitTestRunner() {
- start(AllTests, null, TestRunner.SHOW_TRACE);
- }
- }
- }
3) Create an .fla file to serve as the test container
In the Flash IDE, File->New->Flash File (Actionscript 3). Save the file as AsUnitExample/AsUnitTestRunner.fla. In the properties window, set the Document class in the properties to be AsUnitTestRunner. Save the file again.
4) Create a sample class for us to run tests on
Paste in the following souce code into AsUnitExample/Example.as:
- package {
- public class Example {
- public function add(num1:Number,num2:Number):Number{
- return num1 + num2;
- }
- }
- }
5) Create a test for our sample class
In the Flash IDE, File->New->ActionScript File. Save the file as AsUnitExample/ExampleTest.as. Paste in the following:
- package {
- import asunit.framework.TestCase;
- public class ExampleTest extends TestCase {
- private var _instance:Example;
- /**
- * Constructor
- *
- * @param testMethod Name of the method to test
- */
- public function ExampleTest(testMethod:String) {
- super(testMethod);
- }
- /**
- * Prepare for test, create instance of class that we are testing.
- * Invoked by TestCase.runMethod function.
- */
- protected override function setUp():void {
- _instance = new Example();
- }
- /**
- * Clean up after test, delete instance of class that we were testing.
- */
- protected override function tearDown():void {
- _instance = null;
- }
- /**
- * Test of whether or not class properly instantiated
- */
- public function testInstantiated():void {
- assertTrue("Example instantiated", _instance is Example);
- }
- /**
- * Test that is born to lose.
- */
- public function testFail():void {
- assertFalse("failing test", true);
- }
- /**
- * Test the addition method on example
- */
- public function testAddition():void {
- var result:Number = _instance.add(2,3);
- assertEquals("Expected:5 Received:"+result, result, 5);
- }
- }
- }
6) Create the test suite
In the Flash IDE, File->New->ActionScript File. Save the file as AsUnitExample/AllTests.as. Paste in the following:
- package {
- import asunit.framework.TestSuite;
- public class AllTests extends TestSuite {
- public function AllTests() {
- super();
- addTest(new ExampleTest("testInstantiated"));
- addTest(new ExampleTest("testAddition"));
- //addTest(new ExampleTest("testFail"));
- }
- }
- }
7) In the Flash IDE, compile AsUnitTestRunner.fla by running Control->Test Movie
Cheers!
浙公网安备 33010602011771号