从dll访问冲突中提升测试用例

时间:2013-06-03 13:09:32

标签: c++ boost dll microsoft-metro

我想在Windows RT下从dll启动Boost测试用例。我使用以下命令行通过Visual Studio命令提示符将测试用例构建为dll:

cl.exe / EHsc / D_USRDLL / D_WINDLL / LDd~location \ testcase.cpp~library location \ libboost_unit_test_framework -vc110-mt-sgd-1_53.lib / link / DLL / OUT:~output directory \ testcase.dll

将其放入我的应用程序的文件夹中,并将属性“Content”设置为“true”。启动我的应用程序后,我有以下错误:

TestApp.exe中0x00B9AF16的未处理异常:0xC0000005:访问冲突读取位置0x00000000

调用堆栈的顶部如下:

> TestApp.exe!boost::unit_test::framework::get(unsigned long id, boost::unit_test::test_unit_type t) Line 388 C++ TestApp.exe!boost::unit_test::framework::get(unsigned long id) Line 73 C++ TestApp.exe!boost::unit_test::traverse_test_tree(unsigned long id, boost::unit_test::test_tree_visitor & V) Line 232 C++ TestApp.exe!boost::unit_test::traverse_test_tree(const boost::unit_test::test_suite & suite, boost::unit_test::test_tree_visitor & V) Line 207 C++ TestApp.exe!boost::unit_test::traverse_test_tree(unsigned long id, boost::unit_test::test_tree_visitor & V) Line 234 C++ TestApp.exe!boost::unit_test::framework::run(unsigned long id, bool continue_test) Line 403 C++ TestApp.exe!boost::unit_test::unit_test_main(boost::unit_test::test_suite * (int, char * *) * init_func, int argc, char * * argv) Line 185 C++

这是dll代码(注意:如果我将相同的代码直接放入我的源代码,它可以正常工作):



    void test_stat()
    {
        //some code there
    }

    extern "C" {
        __declspec (dllexport) test_suite* init_unit_test_suite( int argc, char* argv[] )
        {
            test_suite *test = BOOST_TEST_SUITE("test name");
            test->add(BOOST_TEST_CASE(&test_stat));
            return test;
        }
    }

启动测试用例的应用程序代码:



    boost::unit_test::test_suite* main_global_test_suite;

    test_suite* init_unit_test_suite( int argc, char* argv[] ) {
        return NULL; }

    test_suite*  run_global_test_suite (int, char* []) {
        return main_global_test_suite;
    }

        HINSTANCE hMyDll;
        typedef test_suite* (*PFN_MyFunction)(int,const char*);
        PFN_MyFunction pfnMyFunction;
        test_suite* rPtr;

        if((hMyDll=::LoadPackagedLibrary(L"testcase", 0))==NULL) 
        {
            return; 
        }
        pfnMyFunction=(PFN_MyFunction)GetProcAddress(hMyDll,"init_unit_test_suite");

        if (pfnMyFunction != NULL)
        {
        //just create fake arguments for the boost::unit_test::unit_test_main function call
                char* argv[1024];
                    argv[0] = "Text";

                rPtr = pfnMyFunction(1, NULL);
                main_global_test_suite = rPtr;

                    const int error =
        boost::unit_test::unit_test_main(&run_global_test_suite, 1, argv );
        }
        else
        {
                //handling code
        }
        FreeLibrary(hMyDll);

有什么想法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

检查console_test_runner正在做什么。这是命令行应用程序(Boost.Test的一部分),旨在实现这一点 - 加载和执行在共享库中实现的测试单元。另外请确保告诉UTF您要构建DLL:定义BOOST_TEST_DYN_LINK。