Menu

a question about cppunit

Help
QI Jiang
2005-08-23
2013-04-22
  • QI Jiang

    QI Jiang - 2005-08-23

    hello, everyone. I have a question about cppunit
    I has write some test class like this (on linux):

    // TestA.cpp
    class TestA : public CppUnit::TestFixture
    {
        CPPUNIT_TEST_SUITE(TestA);
        CPPUNIT_TEST(testA);
        CPPUNIT_TEST_SUITE_END();

    public:
        void testA();
    };

    // TestB.cpp
    class TestB : public CppUnit::TestFixture
    {
        CPPUNIT_TEST_SUITE(TestB);
        CPPUNIT_TEST(testB);
        CPPUNIT_TEST_SUITE_END();

    public:
        void testB();
    };

    CPPUNIT_TEST_SUITE_REGISTRATION(TestA);
    CPPUNIT_TEST_SUITE_REGISTRATION(TestB);

    I compile TestA&TestB in a library libmytest.a, then I write a main.cpp
    int main( int argc, char **argv)
    {
        CppUnit::TextUi::TestRunner runner;
        CppUnit::TestFactoryRegistry& registry = CppUnit::TestFactoryRegistry::getRegistry();
        runner.addTest(registry.makeTest());
        bool wasSucessful = runner.run("", false);
        return wasSucessful;
    }

    I compile main.cpp use:
    g++  -I./lib -I/usr/local/cppunit/include -c main.cpp -o main.o
    g++ -o MyTest main.o -L./lib -lmytest -L/usr/local/cppunit/lib -lcppunit -static

    then I run the test:

    OK (0 tests)

    you see, TestA & TestB is not running, what is the problem? but if I don't compile TestA&TestB in a library, just compile TestA.cpp,TestB.cpp&main.cpp together, it work:

    OK (2 tests)

    Sorry, because my english is not very well, hope you can understand what I mean:-)
    Best regards

     
    • Code Guru

      Code Guru - 2005-08-24

      The commands you use to compile the code does not include compiling TestA.cpp or TestB.cpp.  You need to compile both of these into TestA.o and TestB.o and include them in the final command that links the executable.

      Are you typing the commands manually or are you using a build tool like make?  I strongly suggest you use a makefile to help with compiling your test cases.

      I hope this helps.

       
    • QI Jiang

      QI Jiang - 2005-08-25

      thanks, Layne.
      I compile use make, the Makefile like this:

      part of Makefile for compile libmytest.a(TestA.cpp&TestB.cpp has been compile into the libmytest.a)

      INCLUDE = -I$(CPPUNIT_ROOT)/include -I$(LOG4CPP_ROOT)/include
      LIB     = -L$(CPPUNIT_ROOT)/lib -lcppunit \           -L$(LOG4CPP_ROOT)/lib -llog4cpp \           -lpthread -static

      OBJS = $(patsubst %.cpp,%.o,$(wildcard *.cpp))

      TARGET = libmytest.a

      .PHONY:all clean

      all:$(TARGET)

      $(TARGET):$(OBJS)
          @rm -rf $@
          ar rcs $@ $^

      %.o:%.cpp %.h
          $(CXX) $(CFLAGS) $(INCLUDE) -c $< -o $@

      part of  Makefile for compile main.cpp

      INCLUDE = -I./lib -I$(CPPUNIT_ROOT)/include -I$(LOG4CPP_ROOT)/include
      LIB     = -L./lib -lmytest -L$(CPPUNIT_ROOT)/lib -lcppunit \           -L$(LOG4CPP_ROOT)/lib -llog4cpp \           -lpthread -static

      OBJS = $(patsubst %.cpp,%.o,$(wildcard *.cpp))

      TARGET = MyTest

      .PHONY:all clean

      all:$(TARGET)

      $(TARGET):$(OBJS)
          $(CXX) -o $@ $^ $(LIB)
          @strip $@

      %.o:%.cpp %.h
          $(CXX) $(CFLAGS) $(INCLUDE) -c $< -o $@

      you see, I have compile TestA.cpp&TestB.cpp in libmytest.a, but it didnot work, I run the TestMain program, it always said "OK 0 (tests)".

       
    • Marco

      Marco - 2006-02-18

      Qi--

      I think I see the problem: In your main.cpp, I don't see any code that forces
      the linker to pull in anything from mylib, so none of your tests are linked
      in, so the runner sees nothing to do.

      I'd suggest moving the last two lines of TestB.cpp:

      CPPUNIT_TEST_SUITE_REGISTRATION(TestA); 
      CPPUNIT_TEST_SUITE_REGISTRATION(TestB); 

      into the start of main.  (Of course, you'll need suitable .h files, etc.)

      --zig

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.