Test Driven Development with C++

青春壹個敷衍的年華 提交于 2019-12-03 03:47:08

问题


Looking to start doing TDD in C++. I've seen CPPUnit, but I was wondering if there are other options that people prefer?

Thanks for your suggestions!


回答1:


I can recommend Google Mock. It comes with a copy of Google Test bundled. We switched from UnitTest++ too Google Test/Google Mock a couple of years ago and have never looked back.

Google Mock can be used even if you don't want to use the mocking facilities. Its matchers are very useful.




回答2:


I switched from CppUnit to boost::test some years ago and I'm much happier with it.

  • Documentation for CppUnit is nonexistent. Good luck trying to find out what command line options it supports without reading the code. Apparently it makes more sense to people already familiar with JUnit though. boost::test has excellent documentation.
  • boost::test's auto test registration facility makes adding unit test cases insanely easy. With CppUnit you have to write quite a lot of boilerplate for each test case (a line in the header and a line it the .cpp to register it, on top of the test method itself).
  • boost::test lets you select test subsets by regexp from the commandline. We had to hack CppUnit sources to get it to do that when we originally picked it up.
  • The one thing I do miss from CppUnit is its "Protectors". You can define your own and have them wrap each test and check whatever (e.g we had a problem with some code messing with the x87 floating point rounding mode; checking the state was unchanged in a Protector quickly caught all offenders). boost::test has some similar thing called a test_observer but last time I tried you couldn't actually fail a test from one.



回答3:


If you are just looking for C++ unit test frameworks, see this question and its answers: C++ unit testing framework



来源:https://stackoverflow.com/questions/5307627/test-driven-development-with-c

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