Question

I am developing a DLL in C++ and want to perform unit testing of that DLL using the Boost Test Libraries. I read the Boost test manual thoroughly but since I am new, I have the following question:

Should I add test classes in the same VC project in which I am developing my DLL?. Ideally I want to do this but I am confused that a DLL has no main() and, on the other hand, the Boost test needs its own main() to execute. So where does the Boost test output go in this scenario? (In fact, I practically implemented this and don't see any output :( and almost spent two days I figuring out the problem, but didn't succeed)

Regards,

Jame.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top