4 Replies Latest reply on Feb 27, 2011 10:56 AM by Wrataxas

    Organizing code for unit tests

    Wrataxas Level 2

      How do people organize the code for unit tests with respect to the application's code?  I first tried to have a separate project for tests, but even with having that project depend on the application project, the test code couldn't find application classes to test them.  Based on something I read, it seems that's because the classes are in a swf and not a swc.

       

      I then put the tests in the same project as the application, which seems to work.  But I get this warning that I don't know how to get rid of:

      Slam is a module or application that is directly referenced. This will cause Slam and all of its dependencies to be linked in with tests:FlexUnitApplication. Using an interface is the recommended practice to avoid this.
      Where Slam is the application I am trying to test.  I don't want an interface; the tests need to test the actual code.  I have to have the direct reference.  At least if I understand what the warning is trying to tell me.
      Anyone know how to make the warning go away?
        • 1. Re: Organizing code for unit tests
          injpix Level 3

          I prefer to have my test code separate from my application's code.  Just as you mentioned, to have a separate project from the application project.  But I have never had that issue, that you mentioned, "...test code couldn't find application classes to test them".  In the test project, I would add the application's src folder to its source code.  And that's it.   Once that test project has reference to those classes, it can instantiated them without a problem.

           

          I like this separation because my application source code doesn't get cluttered with test classes and I am able to automate builds easier with Ant.

          • 2. Re: Organizing code for unit tests
            Wrataxas Level 2

            Thanks for the reply and that seems like a great idea.  I haven't been able to make it work.  My application project is Slam and my tests project is SlamTests.  I first tried, on SlamTest/src, adding a New Folder --> Advanced --> Link to folder in the filesystem and picking Slam/src.  So I ended up with SlamTests/src/src/ having all my application source code linked.  But code completion and building doesn't find the files there when I try to type a class name, or an import.  I tried refreshing, cleaning, closing and reopening the project, restarting Eclipse.  None of that worked.

             

            Next I tried, on SlamTests/src, adding NewFolder --> Advanced --> Link to folder in the filesystem and picking Slam/src/document.  So I ended up with SlamTests/src/document/ which seems to make more sense as then the application source code package names will match the file path in SlamTests.  But again, code completion and building don't find the linked files.

             

            It's as if the files in a linked folder aren't looked at at all. Which doesn't make sense.  So I must be doing something wrong.  Anyone have a clue what that might be?

             

            Thanks.

            • 3. Re: Organizing code for unit tests
              injpix Level 3
              It's as if the files in a linked folder aren't looked at at all. Which doesn't make sense.

              That is a very accurate statement when you *link* a folder or file in Eclipse (eg. FlashBuilder).  In Eclipse, you can as you already know, link folders into its Workspace.  A good reason for this, is to give the developer the ability to open up the linked files thru Eclipse.  And hopefully then Eclipse has an editor for that file type.  For instance at work, all of my Flex projects are in my Eclipse Workspace (this is typically).  But, my project's asset and dependencies files (.xml, .fla, .swf, .swc, .f4v, Flash's asset files .as, etc.) are in a whole separate directory on my file system.  So by linking these files, I am able to open them up thru Eclipse, usually its just the .xml files.  Also, if you are debugging your Flex application and it loads an external .swf file, by having its src folder linked, you can set-up breakpoints for the Debugger.

               

              But what linking doesnt do is import code into your Flex (test) application when it gets compiled.  What you need to do to import code into the Flex application for compilation is the following (I'm recalling these steps from memory, so hopefully its correct):

               

              • in Eclipse, select your project in the 'Project Navigator' panel (typically the panel on the left)
              • right-click on the project to bring up a context menu
              • in this context menu, select the 'Properties' item to bring up a pop-up
              • in this pop-up, select the 'Flex Build Path'
              • the right side of this pop-up will update and select the 'Source Path' tab

               

              This is where you can add your applications src code from your test project.  And also, the other tab 'Library Path', you can add a .swc file anywhere your computer can access .swc file(s).  So by adding your application's src code to the test project this way, you can compile those referenced files in the test runner.

              • 4. Re: Organizing code for unit tests
                Wrataxas Level 2

                Super, perfect, wonderful!  Works just fabulously.  Thanks a bunch.