|
|
|
List of Entries ... - Getting Started With AsUnit 3.0 and FlashDevelop3 in AS3 (Unit Tests)This is a guide to get AsUnit to work with FlashDevelop. I assume that you have a working installation of FlashDevelop up and running. If not you may use this guide http://www.theyak.net/flashdevelop/ to get there. For this guide I used FlashDevelop 3.0.0 Beta7 and AsUnit 3.0 2007-10-11 version (release 162). 1. Download AsUnitGo to http://www.asunit.org and download the ?Framework?. It will contain all the classes and sources you will need to get your unit-testing framework going. 2. Setting up a projectStart FlashDevelop and select ?New Project ?? in the ?Project? Menu. Create a new AS3 project, specify a directory to create the project in and name your project. I called it MapMeister, don't ask why. ;) In the project?s directory create a folder for the sources (e.g. ?src?) and one for the binaries (e.g. ?bin?). The two directories should appear in the ?Project Manager?-Control. If you cannot see the the control, you can switch it on by activating the ?Project Manager? checkbox from the ?View? menu. Now, right-click on the source folder (?src?) and choose ?Add? and select ?New Class?? from the pop up. Enter ?Main.as? in the input field and hit Ok. The class stub of the main class will be added to the sources. Starting now will cause a build failure with a FlashDevelop: ?This project does not have a main class? error.To fix this right-click on the ?Main.as? (the as-file that contains the Main class) in the ?Project Manager? and turn ?Always Compile? on. The main class needs to extend flash.display.Sprite, so it should look like this:
Afterwards, open the ?Properties? tab from the ?Project? menu. Open the output tab and enter the name of the binary to be created including the binary folder, e.g. ?bin\AsUnitTest.swf?.
Image 1 - Setting output file in FlashDevelop 3 3. Adding AsUnit to your projectI assume you have already downloaded the AsUnit-framework archive. Extract it into a convenient directory. Afterwards, add the framework?s AS3 sources to the classpath of your project. To do so open the ?Properties? window again from the ?Projects? menu. Select the ?Classpaths? tab and hit the ?Add Classpath ?? button. Add the ?as3? directory of the framework.
Image 2 - Setting classpath in FlashDevelop 3 for AsUnit Well, I personally recommend to create a new directory for the test cases. Right-click on the project in the ?Project Explorer? and select ?Add?? and then ?New Folder ??. Call it e.g. ?testsrc?.
Image 3 - FlashDevelop's Project Manager shows the project structure Now, it?s time to write the first class, test suite. Right-click on the test-source folder and add a new class called ?AllTests.as?. This class needs to extend asunit.framework.TestSuite, so it should like that:
This class will later contain references to all our tests. This class needs to be passed to the AsUnit framework within the Main-class? constructor like this (Please be aware that in this listing we are passing the stage object to the TestRunner class. This is not part of the original implementation. This is a workaround for a runtime error that occurs with AsUnit and FlashDevelop [check problems section at the end of this tutorial].):
If you start the project now, you will see the gray AsUnit screen. This means you are ready to go! 4. Writing a testAdd another class to the test-source folder. e.g. ?TestFirstTry.as?. This class needs to extend asunit.framework.TestCase. Well, for every class you want to test, create a test class here. Add methods to the test class with each method covering a certain aspect of the functionality. Your first test may look like that:
For the test to be executed, you need to add a reference to the AllTests-class like this:
Now you may run the project. If everything is ok, you should see an AsUnit error message stating that one test failed.
Image 4 - AsUnit Failure message Change the "8" in the TestFloatMath test into a "9" and the failure disappears. You?re ready to go. Get them, tiger! 5. Problems5.1 TypeError: Error #1009: Cannot access a property or method of a null object reference. (TestRunner.as:51)
The reason for this problem is that the TestRunner is not added to the stage. If you do not want to add the TestRunner to the stage,
here is what you can do to solve the problem:
(3) Replace all occurences of "stage" within the file TestRunner.as with "m_parent", which should give you something like this (only the altered methods are shown):
This should fix that problem. :) But it's easier to just add the TestRunner to the stage in Main.as. (Thanks to John for pointing that out.) Postet on 2009/1/7 |
Kommentare / Comments:
(2009.02.20 17:22:38) Sachin Shinde:
Nice tutorial. I followed all steps as you have mentioned in your tutorial, but unfortunately I have ended up with this runtime error, any help?
TypeError: Error #1009: Cannot access ;a property or method of a null object reference.
at asunit.textui::TestRunner/addedHandler()[C:\Tools\asunit\as3\asunit\textui\TestRunner.as:51]
at flash.display::DisplayObjectContainer/addChild()
at asunit.textui::TestRunner/setPrinter()[C:\Tools\asunit\as3\asunit\textui\TestRunner.as:114]
at asunit.textui::TestRunner/doRun()[C:\Tools\asunit\as3\asunit\textui\TestRunner.as:91]
at asunit.textui::TestRunner/start()[C:\Tools\asunit\as3\asunit\textui\TestRunner.as:80]
at Application()[C:\home\IkernelAppAS3\src\Application.as:40]
(2009.02.20 18:14:42) Manuel:
Hi, thanks. Well, your error there is a bit strange. It would mean, that the Flash player cannot find the stage object, which is a actually global variable that really should be there.
Anyway, by looking at our version of AsUnit I see that I might have had the same problem. I'll extend the tutorial to explain what I did to solve the problem.
(2009.02.20 19:09:48) Manuel:
Alrighty, I extended the tutorial to include the fix. I hope it'll help.
Cheers,
Manuel
(2009.03.13 18:31:00) Sachin Shinde:
Hi Manuel,I have followed your fix to TestRunner.as but now I am getting error as follwing... help plz
[exec] C:\home\IkernelAppAS3\testsrc\AppTestsRunner.as(22): col: 19 Error: No default constructor found in base class asunit.textui:TestRunner.
[exec]
[exec] public function AppTestsRunner() :void
(2009.03.13 18:34:33) Sachin Shinde:
FYI, my main test class looks like
public class AppTestsRunner extends TestRunner
{
private var logger:QuickLogger = new QuickLogger("AppTestsRunner");
public function AppTestsRunner(stage) :void
{
SLogger.sc = new SmartCallback(this, startTests, null);
SLogger.init("workstn107", 4444);
SLogger.sock.addEventListener(Event.CONNECT, this.startTests);
}
public function startTests(success:Boolean):void {
logger.log("Connected!! " + success);
start(AllTests);
}
}
(2009.03.13 21:19:44) Manuel:
Hi Sachin,
the problem is your declaration of the constructor of the AppTestsRunner. It should actually be:
public function AppTestsRunner(stage:Stage) :void
There is no type defined for the parameter.
I hope that helps.
Cheers,
Manuel
(2009.04.13 08:28:56) bala:
Hi Manuel,
Can u please help in writing a unit test case on events
Regards
bala
(2009.04.14 22:40:49) Manuel:
Hi bala,
unit tests on event are a riddle for me, too. I do know that there are classes in the AsUnit that are addressing that subject. Until now, I was able to somehow avoid testing of events. It's just one of those things I was just to lazy to investigate. But since you're asking ... I'll look into the subject. Maybe I can find out more.
Regards, Manuel
(2009.04.27 00:56:58) John:
Hi Manuel,
Nice Tutorial. I noticed that in your last steps that instead of changing up the TestRunner.as with m_parent. you can simply addChild the unittests var on the Main.as.
(2009.04.27 11:32:55) Manuel:
Hi John,
you are totally right. TestRunner extends Sprite! Why did this not occur to me? This makes so much more sense! Thanks for the info. I'll update the tutorial asap.
Thanks! :)
Cheers,
Manuel
(2009.05.25 15:14:59) Panel:
Nice post, thx
(2009.08.06 13:35:06) rahel :
"Commenter" is not even in the dictionary.
Kommentieren / Comment: