6 Replies Latest reply on Jun 23, 2010 5:56 PM by sl_church@yahoo.com

    Rerunning Unittests

    sl_church@yahoo.com

      I am using FlexUnit4 with <flexunit:TestRunnerBase id="testRunner" xmlns:flexunit="http://www.adobe.com/2009/flexUnitUIRunner"> and it works fine. I want to give the user an option to rerun the unit tests and produce new "TestRunnerBase" output but can't find a way to do it.  I've tried creating new FlexUnit4 core instances and TestRunnerBase instances and manually adding the TestRunnerBase to my UI (i.e. (VBOX) testRunnerContainer.removeAllcCildren(); testRunnerContainer.addChild(testRunner)) but all I get is running tests with the tests never being completed (my unit tests currently consist of one test that allways passes);

       

      Is there a way to clear the results from TestRunnerBase so I can rerun the tests?

       

       

       

      Thanks,

       

       

      Shawn

        • 1. Re: Rerunning Unittests
          mlabriola Level 4

          Hi Shawn,

           

          The TestRunnerBase is a piece of legacy code that we modified slightly to work with FlexUnit 4 so that users would have a familiar environment if they had been working with FlexUnit .9. Unfortunately, we also inherited its issues. Internally it was never intended to be run more than once. You can do it, but you are going to need to jump through a few hoops:

           

          Let's pretend you have a button that start's the tests:

           

          import mx.events.FlexEvent;

          import org.flexunit.flexui.TestRunnerBase; //BE SURE TO IMPORT THE RIGHT TestRunnerBase (not our fault, really)

          import org.flexunit.runner.FlexUnitCore;

           

           

          private var core:FlexUnitCore;

          private var testRunnerBase:TestRunnerBase;

           

          protected function button1_clickHandler(event:MouseEvent):void

          {

            testRunnerBase = new TestRunnerBase();

            testRunnerBase.percentWidth = 100;

            testRunnerBase.percentHeight = 100;

            testRunnerBase.addEventListener(FlexEvent.CREATION_COMPLETE, handleCreationComplete, false, 0, true );

            addChild( testRunnerBase );

          }

          //You are waiting until the testRunnerBase is ready, then we will start the tests. It has a lot of problems if you start running tests before it is ready
          protected function handleCreationComplete( event:FlexEvent ):void {
            core = new FlexUnitCore();
            core.addListener( testRunnerBase );
            core.run( compilationSuite.SuitesToRun );
          }

          //Now go ahead and create a core, add the testRunnerBase as a listener and run it.

          //To run again, you need someway to cleanup, so pretend another button calls this:

           

          protected function button2_clickHandler(event:MouseEvent):void

          {

            core.removeListener( testRunnerBase );

            removeChild( testRunnerBase );

            testRunnerBase = null;

          }

          You remove the testRunnerBase as a listener, which is important as there are some GC issues in there.
          remove the testRunnerBase and set it to null

          Mike

          1 person found this helpful
          • 2. Re: Rerunning Unittests
            sl_church@yahoo.com Level 1

            I tried something like this.  The main difference was I used a local function (defined inside the start test handler).  And I used the TestRunnerBase tests complete (sic) event to remove the event handlers (I removed the COMPLETION_COMPLETE handler too even though I'm pretty sure it would of been GC'd when I ran the next test.

             

            I'll try it this way,  with a instance-method handler (is there a weak reference issue maybe? My COMPLETION_COMPLETE handler DID get called.)

             

             

            Thnaks,  I'll post my results,

             

             

            Shawn

            • 3. Re: Rerunning Unittests
              sl_church@yahoo.com Level 1

              Re-reading my post I'm not sure I made myself clear my code was someting like this (unfortunelty I did not commit before changing back to the way I had it before so this is untested):

               

              private var core:FlexUnitCore;

              private var  testRunner:TestRunnerBase;

              private var view: TestsView = null;    # Iniitialized in a APPLICATION_COMPLETE handler.

               

              public function doUnitTests(event: MouseEvent): void {

                   function onCreationComplete(event: FlexEvent): void {

                        testRunner.removeEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);

                        testRunner.addEventListner(TessCompleteEvent.TESTS_COMPLETE, on TestsComplete);

               

                        core = new FlexUnitCore();

                        core.addListener(  testRunnerBase );
                        core.run( compilationSuite.SuitesToRun );

                  }

               

                  function onTestsComplete(event: TestsCompleteEvent): void {

                        core.removeListener( testRunner );

                        testRunner.removeEventListnet(TestsCompleteEvent.TESTS_COMPLATE, onTestsComplete);

                        startTestsButton.enabled = true;

                   }

               

                   // I'm in a seperate module so I have a class to the view which contains the test runner:,

                   // Maybe I should just do this in a <fx:Script> block  in the view.

                   // The FlexRunnerBase is the VBOX container's only child so:

               

                   startTestsButton.enabled = false;

                   view.testRunnerContainer.removeAllChildren();;

                   testRunner = new TestRunner();

                   testRunner.addEventListner(FlexEvent.CREATION_COMPLETION, onCreationComplete);

                   view.testRunnerContainer.addChild(testRunner);

              }

              • 4. Re: Rerunning Unittests
                mlabriola Level 4

                hi

                 

                the code i sent is from a project i wrote to test this out for you. it worked well. have you tried to replicate my approach exactly? if it doesnt work we will neeed to know what is different about the two environments.

                 

                mike

                • 5. Re: Rerunning Unittests
                  sl_church@yahoo.com Level 1

                  Have not had time yet but will do now and reply.

                   

                   

                   

                   

                  Thanks again,

                   

                  Shawn

                  • 6. Re: Rerunning Unittests
                    sl_church@yahoo.com Level 1

                    Well I tried it your way,   adding the code to my script block in the view that contained the flex runner and it worked fine.  Then I tried the code I posted above in my TestController.as class and it worked fine too.   I'm not sure what I did last night but I suspect the bug  was "Errror 2AM: You are working when you should be in bed".  So everything is fine.

                     

                    Thanks very much for your help,

                     

                     

                    Shawn

                     

                    For reference the working code is (using Swiz framework):

                     

                         .--- Other Code Here  ---


                            // This event is dispatched by runClientTests button on control bar
                            [Mediate("RunTestEvent.START_CLIENT_TESTS")]
                             public function runClientTests(): void {
                            
                                  function onCreationComplete(event: FlexEvent): void {
                                       testRunner.removeEventListener(FlexEvent.CREATION_COMPLETE,
                                           onCreationComplete);
                                       testRunner.addEventListener(TestsCompleteEvent.EVENT_NAME,
                                           onTestsComplete);
                            
                                       core = new FlexUnitCore();
                                       core.addListener(  testRunner );
                                       core.run( AllTests );
                                 }
                            
                                 function onTestsComplete(event: TestsCompleteEvent): void {
                                     core.removeListener( testRunner );
                                     testRunner.removeEventListener(TestsCompleteEvent.EVENT_NAME,
                                             onTestsComplete);
                                     dispatchEvent(
                                             new RunTestEvent(RunTestEvent.CLIENT_TESTS_COMPLETED));
                                  }
                            
                                  dispatchEvent(new RunTestEvent(RunTestEvent.CLIENT_TESTS_STARTED));
                                  view.testRunnerContainer.removeAllChildren();
                                  testRunner = new TestRunnerBase();
                                  testRunner.percentWidth = 100;
                                  testRunner.percentHeight = 100;
                                  testRunner.addEventListener(FlexEvent.CREATION_COMPLETE,
                                          onCreationComplete);
                                  view.testRunnerContainer.addChild(testRunner);
                             }          

                     

                         .--- Other Code Here  ---