How can I add a unit test to a C++ console program in Visual Studio 2012?

末鹿安然 提交于 2020-01-05 08:17:14

问题


How can I add a unit test to a C++ console program in Visual Studio 2012?

From what I've been able to gather from the MSDN the MS unit test support for C++ can't do this at all, (which doesn't surprise me as the MS C++ unit test support was always shocking).

However in earlier versions of Visual Studio you used to be able to unit test C++ code using Visual Assist, but sadly this doesn't support vs2012.

Does anyone know of a C++ unit test framework with some sort of IDE integration that works with vs2012?


回答1:


Visual studio does support integrated unit testing. You need to structure your program correctly though. The way I like to structure my solutions is to have three projects.

  • A .lib project that has my source code in it.
  • An executable project, linked with the .lib. This calls into the .lib in the main() call
  • A test project (exe), linked with the .lib.

Whilst it is possible to use the Visual Studio testing framework, I would recommend Google test. One of the best things about google test is actually Google mock. You can get some integration with this plugin.




回答2:


You can write your tests in C++/CLI and use NUnit



来源:https://stackoverflow.com/questions/16531398/how-can-i-add-a-unit-test-to-a-c-console-program-in-visual-studio-2012

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!