C++ 代码的单元测试 - 工具和方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/91384/
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-27 12:46:46  来源:igfitidea点击:

Unit testing for C++ code - Tools and methodology

c++unit-testingrefactoring

提问by Sakin

I'm working on a large c++ system that is has been in development for a few years now. As part of an effort to improve the quality of the existing code we engaged on a large long-term refactoring project.

我正在开发一个已经开发了几年的大型 c++ 系统。作为提高现有代码质量的努力的一部分,我们参与了一个大型的长期重构项目。

Do you know a good tool that can help me write unit tests in C++? Maybe something similar to Junit or Nunit?

你知道一个可以帮助我用 C++ 编写单元测试的好工具吗?也许类似于 Junit 或 Nunit 的东西?

Can anyone give some good advice on the methodology of writing unit tests for modules that were written without unit testing in mind?

任何人都可以就为没有单元测试而编写的模块编写单元测试的方法提供一些好的建议吗?

采纳答案by Joe Schneider

Applying unit tests to legacy code was the very reasonWorking Effectively with Legacy Codewas written. Michael Feathers is the author - as mentioned in other answers, he was involved in the creation of both CppUnitand CppUnitLite.

将单元测试应用于遗留代码是编写有效处理遗留代码的原因。Michael Feathers 是作者 - 正如其他答案中提到的,他参与了CppUnitCppUnitLite的创建。

alt text

替代文字

回答by agnul

Google recently released their own library for unit testing C++ apps, called Google Test.

Google 最近发布了自己的用于单元测试 C++ 应用程序的库,称为 Google Test。

Project on Google Code

谷歌代码项目

回答by andreas buykx

Check out an excellent comparisonbetween several available suites. The author of that article later developed UnitTest++.

查看几个可用套件之间的出色比较。那篇文章的作者后来开发了UnitTest++

What I particularly like about it (apart from the fact that it handles exceptions etc. well) is that there is a very limited amount of 'administration' around the test cases and test fixtures definition.

我特别喜欢它(除了它可以很好地处理异常等)是围绕测试用例和测试夹具定义的“管理”数量非常有限。

回答by Jonas

Boost has a Testing librarywhich contains support for unit testing. It might be worth checking out.

Boost 有一个包含对单元测试支持的测试库。可能值得一试。

回答by Brian Stewart

Noel Llopis of Games From Withinis the author of Exploring the C++ Unit Testing Framework Jungle, a comprehensive (but now dated) evaluation of the various C++ Unit Testing frameworks, as well as a book on game programming.

Games From Within 的Noel Llopis是Exploring the C++ Unit Testing Framework Jungle 的作者,这是对各种 C++ 单元测试框架的全面(但现在已经过时)评估,以及一本关于游戏编程的书。

He used CppUnitLite for quite a while, fixing various things, but eventually joined forces with another unit test library author, and produced UnitTest++. We use UnitTest++ here, and I like it a lot, so far. It has (to me) the exact right balance of power with a small footprint.

他使用 CppUnitLite 很长一段时间,修复了各种问题,但最终与另一位单元测试库作者联手,并产生了UnitTest++。我们在这里使用 UnitTest++,到目前为止我非常喜欢它。它(对我而言)具有完全正确的功率平衡,占地面积小。

I've used homegrown solutions, CxxTest (which requires Perl), and boost::test. When I implemented unit testing here at my current job it pretty much came down to UnitTest++ vs boost::test.

我使用过自己开发的解决方案,CxxTest(需要 Perl)和 boost::test。当我在目前的工作中实现单元测试时,它几乎归结为 UnitTest++ 与 boost::test。

I really like most boost libraries I have used, but IMHO, boost::test is a little too heavy-handed. I especially did not like that it requires you (AFAIK) to implement the main program of the test harness using a boost::test macro. I know that it is not "pure" TDD, but sometimes we need a way to run tests from withing a GUI application, for example when a special test flag is passed in on the command line, and boost::test cannot support this type of scenario.

我真的很喜欢我使用过的大多数 boost 库,但恕我直言,boost::test 有点过于严厉了。我特别不喜欢它要求您(AFAIK)使用 boost::test 宏来实现测试工具的主程序。我知道它不是“纯粹的”TDD,但有时我们需要一种方法来从 GUI 应用程序运行测试,例如当在命令行中传入一个特殊的测试标志时,boost::test 不能支持这种类型的场景。

UnitTest++ was the simplest test framework to set up and use that I have encountered in my (limited) experience.

UnitTest++ 是我在(有限的)经验中遇到的最简单的设置和使用的测试框架。

回答by icecrime

I'm using the excellent Boost.Testlibrary in conjunction with a much less known but oh-so-awesome Turtlelibrary : a mock object library based on boost.

我将优秀的Boost.Test库与一个鲜为人知但非常棒的Turtle库结合使用:基于 boost 的模拟对象库。

As a code example speaks better than words, imagine you would like to test a calculatorobject which works on a viewinterface (that is Turtle's introductory example) :

代码示例胜过千言万语,假设您想测试一个calculatorview界面上工作的对象(这是 Turtle 的介绍性示例):

// declares a 'mock_view' class implementing 'view'
MOCK_BASE_CLASS( mock_view, view )
{
    // implements the 'display' method from 'view' (taking 1 argument)
    MOCK_METHOD( display, 1 )                   
};

BOOST_AUTO_TEST_CASE( zero_plus_zero_is_zero )
{
    mock_view v;
    calculator c( v );

    // expects the 'display' method to be called once with a parameter value equal to 0
    MOCK_EXPECT( v, display ).once().with( 0 ); 

    c.add( 0, 0 );
}

See how easy and verbose it is do declare expectation on the mock object ? Obviously, test is failed if expectations are not met.

看看在模拟对象上声明期望是多么容易和冗长?显然,如果没有达到预期,测试就会失败。

回答by philsquared

I've just pushed my own framework, CATCH, out there. It's still under development but I believe it already surpasses most other frameworks. Different people have different criteria but I've tried to cover most ground without too many trade-offs. Take a look at my linked blog entry for a taster. My top five features are:

我刚刚推出了我自己的框架CATCH。它仍在开发中,但我相信它已经超越了大多数其他框架。不同的人有不同的标准,但我试图在没有太多权衡的情况下涵盖大部分领域。看看我的链接博客条目的品尝者。我的前五个功能是:

  • Header only
  • Auto registration of function and method based tests
  • Decomposes standard C++ expressions into LHS and RHS (so you don't need a whole family of assert macros).
  • Support for nested sections within a function based fixture
  • Name tests using natural language - function/ method names are generated
  • 仅标题
  • 基于功能和方法的测试的自动注册
  • 将标准 C++ 表达式分解为 LHS 和 RHS(因此您不需要整个系列的断言宏)。
  • 支持基于函数的装置中的嵌套部分
  • 使用自然语言的名称测试 - 生成函数/方法名称

It also has Objective-C bindings.

它还具有 Objective-C 绑定。

回答by David Sykes

CxxTestis a light, easy to use and cross platform JUnit/CppUnit/xUnit-like framework for C++.

CxxTest是一个轻量级、易于使用和跨平台的 C++ 类 JUnit/CppUnit/xUnit 框架。

回答by yrp

UnitTest++, small & simple.

UnitTest++,小而简单。