4 Replies Latest reply on Jan 5, 2011 8:11 AM by Rgrommel

    Test class variables

    Rgrommel

      Currently I'm trying to store a value in a class variable for use in multiple Tests, but this isn't working (I assume the TestSuite is using different instances of the TestClass for each Test).

       

      Something like:

       

      public class DBTest

      (

           private var dbID:int;

           private var dbManager:DBManager;

       

           [Before]

           public function setUp():void {

                dbManager = new DBManager("testDB", "username", "password");

           }

       

           [Test (order=1,async,timeout="5000",description="Testing the creation of a table")]

           public function testTabelCreation():void {

                Async.handleEvent(this, dbManager, DatabaseEvent.TABLE_CREATED, onTableCreated, 5000);

                dbManager.createTable("newTable");

           }

       

           private function onTableCreated(event:DatabaseEvent, passThroughData:Object):void {

                dbID = event.dbID;

           }

       

           [Test (order=2,async,timeout="5000",description="Testing add record")]

           public function testAddRecord():void {

                Async.handleEvent(this, dbManager, DatabaseEvent.RECORD_ADDED, onRecordAdded, 5000);

                dbManager.addRecord(dbID, "name", "value");

           }

       

      The key here is that I have a value that is returned from the table creation test that I need to use for the other tests.  My approach doesn't work - how should I be doing this?  I know I could put it all in a single sequence of tests, but this defeats the purpose.  And in the actual case the testing logic is more complex and there are many different tests that need to use this single value.  I don't want to put the table creation in a [Before] either, because it can be lengthy and I'd rather just do it once and use that single table.  Suggestions on how I should be doing this?

        • 1. Re: Test class variables
          mlabriola Level 4

          The problem is that the Test case (DBTest) itself is recreated every time.  Ideally, tests are supposed to run in isolation and shouldn't have anything to do with each other... state between tests makes them order dependent, etc...

           

          So, if you really want to do this, you would need to store your variable in a static property or in some other class like a singleton.

           

          Other options, you could organize things a bit differently and use a static method marked BeforeClass method, which will only run once before the class is instantiated for the first time... then all of the other tests would run... then an AfterClass method could be used for cleanup.

           

          Mike

          1 person found this helpful
          • 2. Re: Test class variables
            Rgrommel Level 1

            Thanks for the quick answer.  Yes, I figured it was recreating itself for each test.  I definitely like the approach of putting things in the BeforeClass and AfterClass approach as this seems to fit the isolation paradigm more strictly and would like to reorganize things to fit this.

             

            My only issue/question is that I would like this to be a test.  As far as I can tell, things in [Before] and [BeforeClass] cannot be tests themselves.  Is this correct?

            • 3. Re: Test class variables
              mlabriola Level 4

              Correct. The downside of trying to test this way is that you have things that are partially setup and partially test.....

               

              You aren't going to find an easy answer here. Best I can offer is that you have some duplication. You have the create table as a test somewhere in a test case and use it as a test. Using order or another mechanism you can make sure it runs first as a smoke test. In that same test case (as cleanup in After) destroy the table.

               

              Then for your other tests make it part of setup and not a test.

               

              When you have things that are sometimes setup and sometimes tests it is an indication that you are testing a lot of pieces at once. I usually recommend people avoid that, but given what you are trying to accomplish is test a manager, database and methods all at once, I am not sure there is going to be a better way without breaking it down differently.

               

              If you really want it all just to be a test case, then storing the needed info in a static or something else is just as viable. Neither of these approaches is isolated so I wouldn't let that be the decision making factor.

               

              Mike

              • 4. Re: Test class variables
                Rgrommel Level 1

                Thank you very much for the explanation and quick help.