Testing a DLL with Boost::Test?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 13:54:47

You have 3 ways to do this:

  1. You can definitely do what another reply suggest and build your lib as static. I wouldn't recommend this way though.

  2. You can have one or more separate unit test projects in your solution. These projects will link with your library and with either static or shared version of Boost Test library. Each project will have a main either supplied by the Boost.Test library or implemented by you manually.

  3. Finally you have another option and you can put your test cases directly into your library. You'll need to link with shared version of Boost Test. Once your library is built you can use it regularly as you do now plus you'll have an ability to execute test cases built into it. To execute the test case you'll need a test runner. Boost Test supplies one called "console test runner". You'll need to build it once and you can use for all your projects. Using this test runner you can execute your unit test like this:

    test_runner.exe --test "your_lib".dll

    You should understand all the pluses and minuses of this approach. Your unit test code will be part of your production library. It'll make it slightly bigger, but on the other hand you'll be able to run the test in production if necessary.

You could build your DLL as a static library file first. You can then use it to compile your final DLL directly and create an executable that contains your boost tests. Here's an example using boost.build:

lib lib_base
    : # sources
        $(MAIN_SOURCES).cpp  # Sources for the library.
    : # requirements
        <link>static
    : : ;

lib dll_final
    : # sources
        lib_base
        $(DLL_SOURCES).cpp   # Sources for DllMain .
    : # requirements
        <link>shared
    : : ;

unit-test test_exe
    : # sources
        lib_base
        $(TEST_SOURCES).cpp  # Sources for the unit tests.
    : # properties
        <library>/site-config//boost/test
    ;

You do have to be carefull to not have any important logic in your DllMain but that's usually a bad idea.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!