问题
I have been using gtest as my C++ unit-test framework, and it works great for console applications. Does anybody know if it is possible to use this (or which would be a good alternative) for testing an MFC application? I mean, make an automatic test depending on the events of the GUI (text inside text-boxes, buttons clicks and so). I have seen applications like TestComplete, but I would like to know if gtest offers something similar or if there is a free/opensource alternative to start with.
回答1:
I've been trying to do the same thing as well, and so far my solution has been to go back and forth between a windows application and console application:
http://hdrlab.org.nz/articles/windows-development/changing-a-windows-app-to-a-console-app-in-visual-studio/
If the above link is dead, then in property pages,
Linker -> System -> SubSystem -> Choose "Console"
C/C++ -> Preprocessor -> Preprocessor Definitions -> Change "_WINDOWS" to "_CONSOLE"
Then make sure you have a .cpp that has a main() function with all the necessary #includes to test all the backend logic. If you switch back to a windows application, I found that it just ignores your main function without any conflict with WinMain(), so you don't need to do any tedious commenting out or what not. In any case, it's sort of a hacky solution, and I'm still doing some research and experimenting.
回答2:
I would recommend you take a look at Coded UI as an alternative. You can hand-code or record tests and play them back later. If you decide to hand-code, be prepared to use C#. The tool uses C# classes to define and run tests.
来源:https://stackoverflow.com/questions/26058560/mfc-gui-testable-with-google-test