Is it possible to test “internal” class from a c++ dll using MSTest?

前端 未结 3 940
遥遥无期
遥遥无期 2021-02-08 21:52

We are currently trying to add unit testing to our c++ application. The application is made of 30 projects that generate 29 dll and 1 exe. We use MSTest to run our unit test sin

3条回答
  •  灰色年华
    2021-02-08 22:25

    There is no way, whether you're a unit testing framework or something else, to test code that you can't see. A DLL on Windows only exports symbols which have __declspec(dllexport) defined. Any other symbol is treated as internal when the DLL is compiled, and won't be visible to code using the DLL.

    This is important because it means that the linker can optimize, modify or remove code that isn't exported. The code you want to test might not be there at all. It might be there, but in a different form than you expect. The DLL is compiled under a contract that anything declared with dllexport must be present and visible, and anything else just has to work. It doesn't have to be accessible from the outside world.

    That's not a shortcoming of MSTest (even though it has plenty of other shortcomings and is a pretty awful choice for unit testing C++ code)

    If you want to test that code, you have two options:

    • export it with dllexport, or
    • write your unit test code as part of the dll itself.

提交回复
热议问题