Help save net neutrality! Learn more.

setup() not executing in TextFixture?

Jim O
  • 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;
        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
            suiteOfTests->addTest(new CppUnit::TestCaller<QsoTest>(
            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());;
        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.

  • Anonymous - 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.


Log in to post a comment.