Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

setup() not executing in TextFixture?

Help
Jim O
2010-04-21
2013-04-22
  • Jim O
    Jim O
    2010-04-21

    I have returned to C++ after more than a 10 year absence and am using ccpunit for the first time. I have been trying to set up a test environment by following CppUnit - The Unit Testing Library document, and it does not appear that setup is called in TextFixture. Here is my code, first the file qsotest.h, then the file containing main:

    #include "qso.h"
    class QsoTest : public CppUnit::TestFixture {
        Qso *q;
    public:
        void setup() {
            q = new Qso();
        }
    
        void tearDown() {
            delete q;
        }
    
        void testAddField()
        {
    //      q = new Qso();
            q->addField("call", "VA3HJ");
            CPPUNIT_ASSERT(q->at("call") == "VA3HJ");
    //      delete q;
        }
    
        static CppUnit::Test *suite() {
            CppUnit::TestSuite *suiteOfTests = new
                    CppUnit::TestSuite("QsoTestSuite");
            suiteOfTests->addTest(new CppUnit::TestCaller<QsoTest>(
                            "testAddField",
                            &QsoTest::testAddField));
            return suiteOfTests;
        }
    };
    #endif  // QSOTEST_H
    
    #include <TestRunner.h>
    #include "qsotest.h"
    int main(int argc, char **argv) {
        CppUnit::TextUi::TestRunner runner;
        runner.addTest( QsoTest::suite());
        runner.run();
        return 0; 
    }
    

    When I compile and link, then execute, I get a Segmentation fault in testAddField because q is 0. If I comment out the code in setup and teardown, and uncomment the two lines in testAddField, the program executes and I get the result I expect.

    Am I missing something?  I compile and link using g++ on a system running openSuSE 11.2, using whatever version of software that is in the OpenSuSE repositories.

     
  • Otroson
    Otroson
    2010-04-22

    Try to change "void setup()" na "void setUp()".
    C++ is case sensitive…

     
  • Jim O
    Jim O
    2010-04-22

    Thanks. I thought it had to be something simple; I don't know why I missed that.