How can I pass parameter between cases?

Help
Winston Ma
2008-08-14
2013-04-22
  • Winston Ma

    Winston Ma - 2008-08-14

    I have 2 dependent (but can be independent) test cases. I'd like to test them in sequence. However the outputs from the first test will pass as inputs of the second test. But in CppUnit the number will be re-initializedks  after each test cases. So how can I achieve that?

    Thanks for the help!

     
    • Dragos Andronic

      Dragos Andronic - 2008-08-14

      Store the outputs of the first test into a class member variable. You fill it up in the first test and then read it inside the second test (test that the first test succeeds and you have valid info inside that variable before you use it inside the second test)

      E.g. (let the parameter you want to pass be a CString)

      class Test: public CppUnit::TestFixture
      {
      CPPUNIT_TEST_SUITE( TestSuite1);
      CPPUNIT_TEST( Test1 );
      CPPUNIT_TEST( Test2 );
      CPPUNIT_TEST_SUITE_END();

      public:
         Test(void): m_cString(L"")
      {
      }

      void Test1()
      {
      // fill the parameter with something
      m_cString = ResultOfTheFirstFunctionITest();
      }

      void Test2()
      {
      //some test to see if there input data will be valid
      if ( testForValidity(m_cString))
      {
      // use the parameter
      anotherFunctionIWantToTest(m_cString);
      }
      }

      private:
      CString m_cString;
      }

       
    • Winston Ma

      Winston Ma - 2008-08-19

      I tried this method. However the variables got initialized every time when i switch bet'n test cases.

      I grep the variable m_cString on Test2 it's empty. I think cppunit did clear up all the variable value before doing a new test.

       
    • Dragos Andronic

      Dragos Andronic - 2008-08-20

      Sorry .. I gave an invalid advice, my bad (discard my previous post). CppUnit will create an object (in our case on object of type Test) for every test inside the suite - that's why m_cString is empty - it belongs to a different object.

      I guess that the only way to do it is to use static objects:
      In the suite's header:

      static CString cString(L"");
      ...
      void Test1()
      {
      // fill the parameter with something
      cString = ResultOfTheFirstFunctionITest();
      }
      ..
      void Test2()
      {
      //some test to see if there input data will be valid
      if ( testForValidity(cString))
      {
      // use the parameter
      anotherFunctionIWantToTest(cString);
      }
              cString should maintain it's value between the 2 tests.

      Cheers, and sorry again for the previous post :)
      (P.S. Let me know if it worked for you)

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks