How to test complex classes with CppUnit

Help
2008-11-04
2013-04-22
  • Daanen Vincent

    Daanen Vincent - 2008-11-04

    Hi,

    I'm quite new to CppUnit use and I run into problems while designing my tests.

    Let's have a class A
    class A
    {
    public :
        A()
        ~A();
       
        bool Init();
        bool SearchForAllData(std::vector<CData> &);
        bool DataExist(const CData &);
    }

    Using CppUnit, I implement several test cases

    class CTestA : public CppUnit::TestFixture
    {
    protected:
      A m_A;

           // Test
        void InitOK()
        {    CPPUNIT_ASSERT(A.Init() == true);};
       
        void SearchAllOK();
        {   
            std::vector<CData> l_DataVect;
            CPPUNIT_ASSERT(A.SearchForAllData(l_DataVect) == true);
           
            // some test one date found
            CPPUNIT_ASSERT(l_DataVect.size() == 5);
        };
       
        void SearchDataOK();
        {   
            {
                CData l_DataToFind(1,2,3);
                CPPUNIT_ASSERT(A.DataExist(l_DataToFind) == true);
            }
               
            {
                CData l_DataToFind(1,2,1);
                CPPUNIT_ASSERT(A.DataExist(l_DataToFind) == false);
            }
        };

      // def de la test suite
    public :
        // init Test Fixture
        void setUp();
       
        // release data
        void tearDown();

        CPPUNIT_TEST_SUITE(CTestA);
            CPPUNIT_TEST(InitOK);
            CPPUNIT_TEST(SearchAllOK);
            CPPUNIT_TEST(SearchDataOK);
        CPPUNIT_TEST_SUITE_END();
    };

    To succeed, the test case SearchAllOk need the instance of A to be initialized. This is (according to me)
    ensured because InitOK is called before SearchAllOK.

    But, when the test SearchAllOK begins, the instance of A is not initialized anymore.
    One would say that A is initialized in InitOK but the initialization is cancelled when exiting from InitOk (or from the macro CPPUNIT_TEST)

    Is this the 'normal' way to CPPUNIT macros to work ?

    How could I perform unitary testing on such component without repeating the previous operations ?
    e.g SearchAllOK becomes :   
    void SearchAllOK();
    {
        A.Init();    // will pass once InitOK succeed
           
        std::vector<CData> l_DataVect;
        CPPUNIT_ASSERT(A.SearchForAllData(l_DataVect) == true);
           
        // some test one date found
        CPPUNIT_ASSERT(l_DataVect.size() == 5);
    };

    I try to add the testing pipeline in the setUp method and then tests boolean memory flag but it twiced the work and I do not think it's the right way to do it ...

    Thanks for help

    a new CppUnit user ...

    Vince

     
    • stefan

      stefan - 2008-11-04

      Hi Vince,

      yes that's the normal way CPPUnit (and other xUnit testing frameworks) work.

      Each test is created as new instance.
      You can only share data between the tests of one suite by using class variables (e.g. static A a;).

      But you should be aware that is not assured that tests are run in the given order (but as it comes to CppUnit I have never seen a different order of the test).

      You are using m_A as "fixture", so you should initialize it properly in the setUp() and destroy it in the tearDown().

      I hope this helps a bit.

      Stefan.

       

Log in to post a comment.

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

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks