C++单元测试框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87794/
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
C++ unit testing framework
提问by Rob
I use the Boost Test framework for my C++ code but there are two problems with it that are probably common to all C++ test frameworks:
我将 Boost Test 框架用于我的 C++ 代码,但它有两个问题可能是所有 C++ 测试框架共有的:
- There is no way to create automatic test stubs (by extracting public functions from selected classes for example).
- You cannot run a single test - you have to run the entire 'suite' of tests (unless you create lots of different test projects I guess).
- 无法创建自动测试存根(例如,通过从选定的类中提取公共函数)。
- 您不能运行单个测试 - 您必须运行整个“套件”测试(除非我猜您创建了许多不同的测试项目)。
Does anyone know of a better testing framework or am I forever to be jealous of the test tools available to Java/.NET developers?
有谁知道更好的测试框架,还是我永远嫉妒 Java/.NET 开发人员可用的测试工具?
采纳答案by Brian Stewart
I just responded to a very similar question. I ended up using Noel Llopis' UnitTest++. I liked it more than boost::test because it didn't insist on implementing the main program of the test harness with a macro - it can plug into whatever executable you create. It does suffer from the same encumbrance of boost::test in that it requires a library to be linked in. I've used CxxTest, and it does come closer than anything else in C++-land to automatically generating tests (though it requires Perl to be part of your build system to do this). C++ just does not provide the reflection hooks that the .NET languages and Java do. The MsTest tools in Visual Studio Team System - Developer's Edition will auto-generate test stubs of unmanaged C++, but the methods have to be exported from a DLL to do this, so it does not work with static libraries. Other test frameworks in the .NET world may have this ability too, but I'm not familiar with any of those. So right now we use UnitTest++ for unmanaged C++ and I'm currently deciding between MsTest and NUnit for the managed libraries.
我刚刚回答了一个非常相似的问题. 我最终使用了 Noel Llopis 的 UnitTest++。我比 boost::test 更喜欢它,因为它不坚持使用宏来实现测试工具的主程序 - 它可以插入您创建的任何可执行文件。它确实遭受了 boost::test 的相同负担,因为它需要链接一个库。我使用过 CxxTest,它确实比 C++ 领域中的任何其他东西更接近于自动生成测试(尽管它需要 Perl成为你的构建系统的一部分来做到这一点)。C++ 只是不提供 .NET 语言和 Java 提供的反射钩子。Visual Studio Team System - Developer's Edition 中的 MsTest 工具将自动生成非托管 C++ 的测试存根,但必须从 DLL 导出方法才能执行此操作,因此它不适用于静态库。中的其他测试框架。NET 世界也可能具有这种能力,但我对其中任何一个都不熟悉。因此,现在我们将 UnitTest++ 用于非托管 C++,而我目前正在为托管库在 MsTest 和 NUnit 之间做出决定。
回答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 doesn'tdo generation of stubs - but that's a fairly specialised area. I think Isolator++is the first tool to truly pull that off. Note that Mocking/ stubbing frameworks are usually independent of unit testing frameworks. CATCH works particularly well with mock objects as test state is not passed around by context.
它不生成存根——但这是一个相当专业的领域。我认为Isolator++是第一个真正实现这一目标的工具。请注意,模拟/存根框架通常独立于单元测试框架。CATCH 与模拟对象配合得特别好,因为测试状态不是通过上下文传递的。
It also has Objective-C bindings.
它还具有 Objective-C 绑定。
[update]
[更新]
Just happened back across this answer of mine from a few years ago. Thanks for all the great comments! Obviously Catch has developed on a lot in that time. It now has support for BDD style testing (given/ when/ then), tags, now in a singleheader, and loads of internal improvements and refinements (e.g. richer command line, clear and expressive output etc). A more up-to-date blog post is here.
几年前刚刚发生在我的这个答案中。感谢所有精彩评论!显然,Catch 在那段时间发展了很多。它现在支持 BDD 风格测试(given/when/then)、标签,现在在单个标题中,以及大量的内部改进和改进(例如更丰富的命令行、清晰和富有表现力的输出等)。更新的博客文章在这里。
回答by J Miller
Take a look at the Google C++ Testing Framework.
看看谷歌 C++ 测试框架。
It's used by Google for all of their in-house C++ projects, so it must be pretty good.
Google 的所有内部 C++ 项目都使用它,所以它一定非常好。
http://googletesting.blogspot.com/2008/07/announcing-new-google-c-testing.html
http://googletesting.blogspot.com/2008/07/annoucing-new-google-c-testing.html
回答by user49248
Boost.Test does allow to run test case by name. Or test suite. Or several of them.
Boost.Test 确实允许按名称运行测试用例。或测试套件。或其中几个。
Boost.Test does NOT insists on implementing main, though it does make it easy to do so.
Boost.Test 并不坚持要实现 main,尽管它确实很容易实现。
Boost.Test is NOT necessary to use as a library. It has single header variant.
Boost.Test 不需要用作库。它具有单个标题变体。
回答by yrp
I'm a big fan of UnitTest++, it's very lightweight, but does the job. You can run single tests there easily.
我是UnitTest++ 的忠实粉丝,它非常轻巧,但可以胜任。您可以轻松地在那里运行单个测试。
回答by Scott Saad
Great question! A few years ago I looked around forever for something worth using and came up short. I was looking for something that was very lightweight and did not require me to link in some libraries... you know something I could get up and running in minutes.
好问题!几年前,我总是四处寻找值得使用的东西,但结果却不尽人意。我正在寻找一些非常轻量级的东西,不需要我在某些库中进行链接......你知道我可以在几分钟内启动并运行的东西。
However, I persisted and ended up running across cxxtest.
但是,我坚持并最终运行了cxxtest。
From the website:
从网站:
- Doesn't require RTTI
- Doesn't require member template functions
- Doesn't require exception handling
- Doesn't require any external libraries (including memory management, file/console I/O, graphics libraries)
- Is distributed entirely as a set of header files (and a python script).
- 不需要 RTTI
- 不需要成员模板函数
- 不需要异常处理
- 不需要任何外部库(包括内存管理、文件/控制台 I/O、图形库)
- 完全作为一组头文件(和一个 python 脚本)分发。
Wow... super simple!Include a header file, derive from the Test class and you're off and running. We've been using this for the past four years and I've still yet to find anything that I'm more pleased with.
哇……超级简单!包含一个从 Test 类派生的头文件,然后您就可以开始运行了。过去四年我们一直在使用它,但我仍然没有找到任何让我更满意的东西。
回答by Steve Duitsman
Try WinUnit. It sounds excellent, and is recommended by John Robbins.
回答by small_duck
I like the Boost unit test framework, principally because it is very lightweight.
我喜欢 Boost 单元测试框架,主要是因为它非常轻量级。
I never heard of a unit-test framework that would generate stubs. I am generally quite unconvinced by code generation, if only because it gets obsolete very quickly. Maybe it becomes useful when you have a large number of classes?
A proponent of Test Driven Development would probably say that it is fundamental that you run the whole test suite every time, as to make sure that you have not introduced a regression. If running all the tests take too much time, maybe your tests are too big, or make too many calls to CPU intensive functions that should be mocked out? If it remains a problem, a thin wrapper around the boost unit-tests should allow you to pick your tests, and would probably be quicker than learn another framework and port all your tests.
我从未听说过会生成存根的单元测试框架。我通常不相信代码生成,因为它很快就会过时。也许当你有大量的类时它会变得有用?
测试驱动开发的支持者可能会说,每次运行整个测试套件是很重要的,以确保没有引入回归。如果运行所有测试花费太多时间,可能是您的测试太大,或者对应该模拟的 CPU 密集型函数进行了太多调用?如果它仍然是一个问题,那么围绕 boost 单元测试的薄包装应该允许您选择您的测试,并且可能比学习另一个框架和移植所有测试更快。
回答by small_duck
http://groups.google.com/group/googletestframework, but it's pretty new
回答by small_duck
I'm using tut-framework
我正在使用tut 框架