使用 C++ 进行测试驱动开发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5307627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:52:43  来源:igfitidea点击:

Test Driven Development with C++

c++tddcppunit

提问by hahuang65

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

希望开始在 C++ 中进行 TDD。我看过 CPPUnit,但我想知道人们是否还喜欢其他选项?

Thanks for your suggestions!

感谢您的建议!

回答by Tobias Furuholm

I can recommend Google Mock, which has become part of Google Test bundled. We switched from UnitTest++ to Google Test/Google Mock a couple of years ago and have never looked back.

我可以推荐Google Mock,它已成为 Google Test 捆绑的一部分。几年前,我们从 UnitTest++ 切换到 Google Test/Google Mock,并且从未回头。

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

即使您不想使用模拟工具,也可以使用 Google Mock。它的匹配非常有用的

回答by timday

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

几年前我从 CppUnit 切换到 boost::test,我对它更满意。

  • Documentation for CppUnit is nonexistent. Good luck trying to find outwhat 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 facilitymakes 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 youselect 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 onething I do missfrom 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_observerbut last time I tried you couldn't actually fail a test from one.
  • CppUnit 的文档不存在。祝你好运,在不阅读代码的情况下找出它支持的命令行选项。显然,对于已经熟悉 JUnit 的人来说,这更有意义。boost::test 有很好的文档
  • boost::test 的自动测试注册工具使添加单元测试用例变得非常容易。使用 CppUnit,您必须为每个测试用例编写大量样板文件(标题中的一行和 .cpp 中的一行用于注册它,在测试方法本身之上)。
  • boost::test允许您从命令行通过正则表达式选择测试子集。当我们最初选择它时,我们不得不破解 CppUnit 源代码才能做到这一点。
  • 我从 CppUnit 中错过件事是它的“保护者”。您可以定义您自己的测试并让它们包装每个测试并检查任何内容(例如,我们遇到了一些代码与 x87 浮点舍入模式混淆的问题;检查保护器中的状态是否不变很快就抓住了所有违规者)。boost::test 有一些类似的东西叫做 a但上次我试过你实际上不能从一个测试中失败。test_observer

回答by Fred Larson

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

如果您只是在寻找 C++ 单元测试框架,请参阅此问题及其答案:C++ 单元测试框架