最先进的 C++ 单元测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20606793/
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
State of the art C++ Unit Testing?
提问by pepr
What are the most modern approaches to unit testingfor the C++ language? The class of the languages with bigger introspection power (like Python) have unit testing frameworks that are somehow more natural to use. Unit tests can be defined more easily. In comparison, the classical CppUnit
(based on JUnit
) seems to take very conservative approach. Is there anything newer and better that uses the specific power of C++ (or even C++11) to make the life easier?
对 C++ 语言进行单元测试的最现代方法是什么?具有更大内省能力的语言类(如 Python)具有在某种程度上更自然地使用的单元测试框架。可以更轻松地定义单元测试。相比之下,经典的CppUnit
(基于 JUnit
)似乎采取了非常保守的方法。有什么更新更好的东西可以使用 C++(甚至 C++11)的特定功能来使生活更轻松?
I have been using CppUnit
framework for a while in rather simplistic way for parts of the project on Windows native C++ (Visual Studio 2005 and 2010). We did not choose the Test Driven Development approach earlier, because there already was a lot of legacy code, and we had found it quite difficult to add tests for it. We had to refactor the application, but adding all the nice tests would be time consuming even in the case.
CppUnit
对于 Windows 本机 C++(Visual Studio 2005 和 2010)上的部分项目,我一直在以相当简单的方式使用框架。我们之前没有选择测试驱动开发方法,因为已经有很多遗留代码,我们发现为其添加测试非常困难。我们不得不重构应用程序,但即使在这种情况下,添加所有好的测试也很耗时。
Recently, we have switched to Visual Studio 2013 (because of the C++11 standard implementation), and we are going to start new, rather long-term project.
最近,我们已经切换到 Visual Studio 2013(因为 C++11 标准实现),我们将开始一个新的、相当长期的项目。
Having the previous good (small) experience with unit testing, I would like to try the Test Driven Development approach. As the project is not a tiny one (expected size about the same as the older one, i.e. about 200 k lines of code), I prefer rather more easy (but not less capable) framework.
有了之前的单元测试良好(小)经验,我想尝试测试驱动开发方法。由于该项目不是一个小项目(预期大小与旧项目大致相同,即大约 20 万行代码),我更喜欢更简单(但功能不差)的框架。
There is a chance the new project could lead to cross-platform implementation (Windows and Linux). There is a unit testing support in Visual Studio 2013, but I have no experience with it and how would it fit with the cross-platform.
新项目有可能导致跨平台实施(Windows 和 Linux)。Visual Studio 2013 中有一个单元测试支持,但我没有使用它的经验以及它如何适应跨平台。
So far, I have found the list of unit testing frameworks for C++. However, one cannot see how they differ in principle. I currently have three candidates (conservative choice):
到目前为止,我已经找到了C++ 的单元测试框架列表。然而,人们看不出它们在原则上有何不同。我目前有三个候选人(保守选择):
- Boost -- the probable candidate; testbed for C++ standards; hence it is likely it is to be widely accepted; probably the biggest user group. It seems to be more advanced than
CppUnit
. - CppUnit -- I know it, but it writing all of the code around is not a pleasure.
- Visual Studio 2013 built-in -- new to me, somehow can generate the skeletons probably.
- Boost——可能的候选人;C++ 标准的测试平台;因此它很可能被广泛接受;可能是最大的用户群。好像比
CppUnit
. - CppUnit——我知道,但是编写所有代码并不是一种乐趣。
- Visual Studio 2013 内置——对我来说是新的,不知何故可以生成骨架。
Anyway, it seems that all of the three use similar approach. Possibly the VS2013 supports generating the code, but it does not mean it leads to anything simpler.
无论如何,似乎所有三个都使用类似的方法。可能 VS2013 支持生成代码,但这并不意味着它会导致任何更简单的事情。
Is there any radically new approach?
有什么全新的方法吗?
采纳答案by jalf
The only test framework worth considering: Catch
唯一值得考虑的测试框架: Catch
For an introduction to the lib, see also hereand here
It's easy to use (a header-only lib consisting of just one header), portable, and has by far the simplest, cleanest syntax of any C++ unit testing framework.
它易于使用(仅包含一个头文件的头文件库)、可移植,并且具有迄今为止任何 C++ 单元测试框架中最简单、最干净的语法。
And unlike other libraries, you don't need to remember two dozen different macros f or different types of assertions.
与其他库不同的是,您不需要记住两打不同的宏或不同类型的断言。
You just use REQUIRE:
您只需使用 REQUIRE:
int one = 1;
REQUIRE( one == 2 );
which, through some clever operator overloading, will show both the original expression and the expanded argument values in the output:
通过一些巧妙的运算符重载,它将在输出中同时显示原始表达式和扩展参数值:
test.cc(7): FAILED:
REQUIRE( one == 2 )
with expansion:
1 == 43
Compared to this, every other framework is a chore to use IMO.
与此相比,其他所有框架都很难使用 IMO。
I used to use Boost.Test before I found this, but that was a lotmore cumbersome to set up and use. We use CppUnit at work, and that seems to be designed to be as fragile and painful as possible.
我曾经使用如Boost.Test我发现这之前,但那是很多更繁琐的设置和使用。我们在工作中使用 CppUnit,这似乎被设计为尽可能脆弱和痛苦。
I've looked briefly at the VS2013 test framework, but haven't tried it, and it looks tolerable, but very much like it's emulating "the old guard". It doesn't really try to be cleaner, easier or better than CppUnit, Boost.Test and all the other ones that came before Catch. So I'd say don't bother with it. Tests should be easy to write (and to understand), and Catch is lightyears ahead of every other framework I've seen on that front.
简单看了下VS2013的测试框架,没试过,看起来还可以,但很像在模仿“老兵”。它并没有真正尝试比 CppUnit、Boost.Test 和 Catch 之前出现的所有其他工具更干净、更容易或更好。所以我想说不要打扰它。测试应该易于编写(和理解),而 Catch 比我在这方面看到的所有其他框架都领先数光年。
回答by Sean
I've been using theVisual Studio 2013 built in test framework for about 6 weeks now and really like it. The integration is excellent and it's very easy to pick up. If you're working on a project that only targets Windows then I thoroughly recommend it.
我已经使用 Visual Studio 2013 内置测试框架大约 6 周了并且非常喜欢它。集成非常好,很容易上手。如果您正在从事仅针对 Windows 的项目,那么我强烈推荐它。