I've several MFC based projects (DLLs). I want to add some GTest/GMock tests inside.
I need to do it with minimal efford so ma plan was:
prepare a single executable which would load dynamically desired dll with minimal main() like this:
int main(int argc, char** argv)
{
LoadLibrary(argv[1]);
testing::InitGoogleMock(&argc, argv);
const int status = RUN_ALL_TESTS();
return status;
}
The problem is that tests from loaded library are not visible to my exe file (dll is loaded and initialized for sure). The output is:
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[ PASSED ] 0 tests.
It seems like I should somehow introduce tests from dll file into exe but I cannot find any information. Is my approach ok? Or maybe there are other, simple ways?
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:
gmock.lib
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With