how to test method contain dlopen ?

2010-04-16
2013-04-22
  • yongle20078
    yongle20078
    2010-04-16

    Hi all,
    I use cppunit test a class, this class contian an method which call an .so method, it use dlopen and some method.
    after test program finished. can't get the test result.
    Gould you please give me some advise?

    thanks,
    Yong

     
  • yongle20078
    yongle20078
    2010-04-16

    I have three file. first is main.cpp
    # include<stdio.h>
    #include <cppunit/extensions/TestFactoryRegistry.h>
    #include <cppunit/ui/text/TestRunner.h>

    using namespace std;
    int main()
    {
    CppUnit::TextUi::TestRunner runner;

    // 从注册的TestSuite中获取特定的TestSuite, 没有参数获取未命名的TestSuite.
    CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry("alltest");
    // 添加这个TestSuite到TestRunner中
    runner.addTest( registry.makeTest() );
    // 运行测试
    runner.run();
    //std::cout<<"Main is over!"<<std::endl;
    printf("Main is over!\n");

    return 0;
    }

    testhello.h

    #include "cppunit/extensions/HelperMacros.h"
    #include <cppunit/Exception.h>
    using namespace std;

    class WidgetProxyTest: public CppUnit::TestFixture {

    CPPUNIT_TEST_SUITE(WidgetProxyTest);

    //add test case

    CPPUNIT_TEST(testInitialize);

    CPPUNIT_TEST_SUITE_END();

    public:
    void setUp();
    void tearDown();

    void testInitialize();

    };

    testhello.cpp

    #include "testhello.h"
    #include <string>
    #include<dlfcn.h>
    #include<iostream>

    using namespace std;

    CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(WidgetProxyTest,"alltest" );
    //CPPUNIT_TEST_SUITE_REGISTRATION(WidgetProxyTest);
    void WidgetProxyTest ::setUp()
    {
    }
    void WidgetProxyTest::tearDown()
    {     
    }

    void WidgetProxyTest::testInitialize()
    {

    void* handle=dlopen("./hello.so", RTLD_LAZY);
    if(!handle)
    {
    printf("can't open lib\n");
    cout << dlerror()<<endl;
    }
    typedef void (*hello_t)();
    hello_t hello=(hello_t) dlsym(handle, "hello");

    cout << dlerror()<<endl;

    hello();
    dlclose(handle);
    printf("complete\n");
    bool result=true;
    CPPUNIT_ASSERT(result ==true);
    }

    and a dll lib hello.so.  it's code is

    #include<iostream>

    extern "C" void hello()
    {
    std::cout<< "hello"<< '\n';
    }

    I compile these file. and run it.

    the result is

    .complete
    Main is over!

    there is not testcase count.

    some on can help me?

    I think this the cppunit 's  bug