Step-By-Step Unit Testing with ASUnit Part One

Step-By-Step Unit Testing with ASUnit Part One By Tim Beynart 3/09/2007 Introduction This article will explain in detail how to use the ASUnit framework to write unit tests for ActionScript. It is aimed at an experienced AS developer who is curious about unit testing but has been frustrated by the lack of documentation for ASUnit. To understand the concepts and code I present here, you should h a ogp O , n oux t, ern gi r a . tc i a snr oOa c nse i a r h lug …t Aho n v tg sf P y r e un dr a i s t chled e r a sh o c on o d n ny c l e this article is ActionScript 2. There are myriad resources for understanding the philosophy and concepts behind Test Driven De m t D o rwf s tn a bsu ti nt f”d eo e( ) h Ilune t d lfis a “t t e vp n D, e icoh u n oone gd s s o l T s e l o s t tt n ei c r authoring. In the conclusion, I will provide several links and some of my own insights so you can dive deeper into the TDD methodology. This is part one of a two-part article. A simple test example A test case is simply a method in a test class that fires another method on a target class then examines the results. The code below is a very basic example to illustrate how you test the output of an addition method: Import Example; Class ExampleTest{ Public function testAdd():Void{ var example = new Example(); var result = example.add(2,3); If(result!=4){ teEFEE c : ce + r (SADx t5, rev” a“ T I . ed ed cT L pe i: result); } } Class Example{ public function add(num1:Number,num2:Number):Number{ Return num1+num2; } Notice that there is no code in the Example class that has anything to do with testing. Unit tests are distinct from the production code, and test classes exist independently of the classes being tested. The ExampleTest class creates an instance of Example then starts testing it. You use a test case to ensure that the method you are testing behaves the way you expect. Note: Testing is concerned only with public members of a class. The idea is to make sure your class behaves the way anyone using it might expect. You do not (and cannot) write tests for private members. The assumption is that a successful test on a public member assures any relevant private members as well. ASUnit.org: Download and Install ASUnit is available at http://asunit.org/. ASUnit is a framework that provides a more elegant and rigorous test environment than the crude example above, and is based on the conventions set by other ti l r lJ’ n e g t se aU. s pf ias i t a m k v Jt n o On the ASUnit web site, you will see 3 different downloads. One is just the ActionScript classes, the second contains the AsUnit.exe desktop executable, and the third is a Flash IDE extension that creates test cases for you in Flash. I recommend you download the package that includes the AsUnit.exe executable, which is what I will be using in this article. The AsUnit.exe is a desktop program that creates your test classes for you. The download also includes the ASUnit classes for AS2 and AS3 projects. Windows: http://sourceforge.net/project/showfiles.php?group_id=108947&package_id=168969 OSX: http://aralbalkan.com/798 The installer will create an AsUnit directory in your Program Files that contains the executable and a framework directory. The framework directory contains 3 directories: as2, as25, and as3. The as2 directory is there for backwards compatibility with the original ASUnit release. The as25 directory is the latest release for ActionScript 2 testing, and is the one you will be using. It contains asynchronous test cases and several refinements to the original as2 release. And of course the as3 directory is for testing ActionScript 3. Using the ASUnit Framework The following is how I set up my tests, and though you may decide to do thingsfn.ei d rl h s iey rn f tT e ’ e t any right or wrong here, just loose convention and personal preference. Create a folder structure like the following, using a copy of the as25 folder: [Note: In the screen shots I am using Eclipse (http://www.eclipse.org) with the Flash Development Tool (FDT) plugin (http://fdt.powerflasher.com) .] If you are using FDT, add as25, classes, and tests to the classpath. Create the Test Classes The next step is to run the AsUnit.exe program to create the test classes. The program is a bit c t tv o ra p s: o e u e h ’s bt u r i, est y p ni t s e e e ni 1. C oh+e oeot o o , g y pe n e l ne”x tPe d d n d eurc a . i t“n t rc r wa i o ot m c k th j sp n v r ja 2. In the Source Folder area, browse to the course folder for the class you want to test. Keep in mind that the class file does not exist yet! AsUnit.exe will generate the empty class for you. For the example it is C:\...\src\classes\com\example 3. D l“ e S co r r Folder. ee S ao el” T sc a su Fe oest et m r df 4. I ema d d n l“ ict ( h r lrl this instructs n T per o ,e Ao r2 F 7 8 yo) t e ls p wsc c S .l o Pen , h t o et t i 0a np s a y ” the program to use the as25 framework directory. 5. Browse to the folder you want the test classes to be generated in. For the example it is C:\...\src\tests 6. I e sa f , exp,e hplro t. n Csm i t “ me et “ ia bo t lN ely E lsc eu ” i t ha e p a ” lt b d un d c 7. Sc res s sc et a ro t. et e T C ” lt“nr a bo l“ a e a , et S d ” i t e C t t e e h a d d un 8. C “ a n t e sa f . s l t i t t y“ me” l C t e o Csm i Y h d a l h e oE p s i re x t lN elo o g d ga l ux l c e” t h a k e u ue a d o tl s a . a a “ me t we a . nE pe s ect dx ls ” r e d a T. a re 9. Sc e aT Se c “ N ”ohl t i t s “ ti et n te usl R o.uo ga l h a T Se l“ ees i , ku w Y s d d ga y e us eG r tt i n ”c ue a o ts s t C tS eu” reu sl e dcsy a cf. l 10. OK, you are done for now with AsUnit.exe! What Just Happened? AsUnit.exe created 3 classes for us: Example.as, ExampleTest.as, and AllTests.as. Example is a stub file with nothing beyond a constructor. This is a good thing, since you want to write tests before you write any real code. ExampleTest contains the framework setup code to begin writing tests against the Example class. AllTests contains a reference to each test class we produce; if you were to run AsUnit.exe again to create a new test, a reference to it would appear in the updated AllTests. When you create the FLA in the next step, it will utilize the AllTests class to run the testing process. I gd a l a en tc i a ls ga e what the framework t o i t ot g rdd E pe ot i of ’ o eo kt e a o n me t en a s a d o h ee e x T t d requires: class ExampleTest extends TestCase { private var className:String = "ExampleTest"; private var instance:Example; public function ExampleTest(testMethod:String) { super(testMethod); } public function setUp():Void { instance = new Example(); } public function tearDown():Void { delete instance; } public function testInstantiated():Void { assertTrue("Example instantiated", instance instanceof Example); } public function test():Void { assertTrue("failing test", false); } } N : n x nmto tdh u i l a when creating the classes, so make o A ie ’ a ngo de a d s t t s t i sr u a tql csh e U. s e t e h i a s f e p sure you update the Example class definition and the import statement in ExampleTest. This test extends TestCase, which provides assertion methods and the core functionality to run tests. A detailed examination of the ASUnit classes is outside of the scope of this article,t ion bim rt u’ pat t s t o understand how a test suite works. Each test method that is run will use a single, discrete instance of the Example class. So, when testInstantiated is run by the framework, an instance of Example is created to test on in the setup method. When the next test is run, called test, another instance of Example is created. In other words, setup and teardown are fired for each test in the class. If this seems a little fuzzy, it will make more sense when you see some real tests later in the article. Create the FLA to run our tests. N t tt cs r t, n a yc pt . rh t t tFt o h he l s s p u e w t ml e H ’ w s peA w a esa a e y e a o i m es o uhLo t ts e u o d e o e h eo e render ASUnit: 1. Move the file as25/AsUnitTestRunner.as to src/tests and rename it ExampleTestRunner.as. Make sure you update the class definition in the file. 2. Update the ExampleTestRunner class, making sure the main method creates an instance of the class: import asunit.textui.TestRunner; class ExampleTestRunner extends TestRunner { public function ExampleTestRunner() { start(AllTests); } public static function main():Void { var runner = new ExampleTestRunner(); } 3. One s Ee awo c dxpe ueanai t p tFh , a n m i l“ me t nfa s t h ehl I c t e v a E ls nr”dv t e a D re e l a TR . e l eo src/tests directory. 4. In the publish settings, add the classpath to your classes directory. 5. In the main timeline, add the following code. (Since this is a static method and this fla resides in the test directory, you do not need a constructor or import statement.): ExampleTestRunner.main(); Run a Test! Run a test movie. You should see the following screen: B m,a . txi h u etl L e n y m rfd e a e … ii e ’ m w s First of all, look at the method that failed. ASUnit provides the method name for every failed test, which is shown in the dialog as ExampleTest.test(). Open ExampleTest.as and scroll to the test() method. You will see: public function test():Void { assertTrue("failing test", false); } This is a dummy test inserted by the AsUnit.exe to force a failure. Before I explain the syntax, you should understand the mechanism at work here. When the method is called by the framework, it runs an assertion. Assertions (in this case, assertTrue) are test methods provided by the framework and are designed for specific tests. For example, assertTrue will fail if the operation tested returns anything other than true; assertFalse will fail if anything other than false is returned. An assertion requires a string argument for the message to print if it fails, and an operation, object, or objects as arguments depending on the application. To clarify, the method syntax is assertion("failure message", operation); In the example, the message is "failing test", and the operation is false. It fails because, obviously, false is not true, so the swf prints the failure with the custom message. For another example, recreate the addition test from the beginning of the article. In ExampleTest, delete the bogus test method and add the following: public function testAdd():Void { var result:Number = instance.add(2,3); assertTrue("Expected:5 Received:"+result, result==5); } Now in Example you need to write the add method in the Example class: public function add(num1:Number,num2:Number):Number{ return num1+num2; } Run the FLA again and you will see that the tests pass: You should fiddle with the addition test to see what happens when you trigger failures. The assertions available in ASUnit are: assertTrue(msg:Object, assertion:Boolean) assertFalse(msg:Object, assertion:Boolean) assertEquals(msg:Object, assertion1:Object, assertion2:Object) assertSame(msg:Object, object1:Object, object2:Object) assertNotNull(msg:Object, assertion:Object) assertNull(msg:Object, assertion:Object) assertUndefined(msg:Object, assertion:Object) assertNotUndefined(msg:Object, assertion:Object) fail( userMessage:String ) End of Part one Hopefully you can get ASUnit set up using the information in this article. In the next article I will show some real world examples from a set of common classes I use in development. Once you get your head around the mechanics and syntax of unit testing, you can explore its benefits and discover where you can best apply it in your projects. I must thank my colleague Isaac Rivera for teaching me pretty much everything you read in this article.

Related docs
Step-By-Step Unit Testing with ASUnit Part Two
Views: 28  |  Downloads: 3
Step by step
Views: 19  |  Downloads: 0
Step by Step...
Views: 79  |  Downloads: 2
Step by Step
Views: 17  |  Downloads: 0
Step By Step Step By Step
Views: 234  |  Downloads: 36
Step by step
Views: 11  |  Downloads: 0
Step by Step
Views: 29  |  Downloads: 0
STEP by STEP
Views: 15  |  Downloads: 1
STEP BY STEP GUIDE STEP BY STEP STEP BY STEP
Views: 120  |  Downloads: 6
STEP BY STEP
Views: 2805  |  Downloads: 5
A Step by Step Guide
Views: 116  |  Downloads: 8
STEP-BY-STEP
Views: 111  |  Downloads: 9
Step by step
Views: 9  |  Downloads: 0
Step by Step
Views: 34  |  Downloads: 1
premium docs
Other docs by amberp