Tests which will be executed are determined at compile time. What you are doing is simply loading the DLL into process address space using explicit DLL loading (function LoadLibrary
) at runtime.
First of all, I would suggest that you do not add tests to your DLL projects at all. There is no need for unit test code to be inside a DLL which will be deployed to end users. Another problem is that, when you use explicit DLL loading, you have to explicitly retrieve address of a function from the DLL and use it to call the function:
https://msdn.microsoft.com/en-us/library/784bt7z7.aspx
This is not suitable for your what you want. What you should do is the following:
- Add tests (which use functions from all desired DLLs) to project that will build the executable that will run unit tests from all DLLs.
- Add paths of header files for functions and classes (from all desired DLLs) as additional include directories for project that builds this executable.
- Link this executable to
gmock.lib
- Setup implicit DLL loading using
dllimport
storage class attribute, so you do not have to write code that explicitly acquires addresses of functions from the DLL which will be used in your unit tests.
For more information about implicit DLL loading see:
https://msdn.microsoft.com/en-us/library/d14wsce5.aspx
EDIT:
Since switching to implicit linking is not an option for you, modify your approach this way:
First, add an additional exported function, which runs unit tests, to each of your DLLs:
void runTests(int argc, char *argv[])
{
testing::InitGoogleMock(&argc, argv);
const int status = RUN_ALL_TESTS();
}
Then, in the main function of your executable, loop through all DLLs and execute this exported function for each of them using the following:
HMODULE hDLL = LoadLibraryA("MyDLL");
if(hDLL != NULL)
{
fpRunTests runTestsFun = (fpRunTests)GetProcAddress(hDLL, "runTests");
if(!runTestsFun)
{
// Handle the error
FreeLibrary(hDLL);
}
else
{
// Call the function which runs tests
runTestsFun(argc, argv);
}
}