最先进的C ++单元测试? [英] State of the art C++ Unit Testing?

查看:110
本文介绍了最先进的C ++单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于C ++语言,最现代的单元测试方法是什么?具有较大内省能力的语言类(如Python)具有单元测试框架,它们以某种方式更自然地使用。单元测试可以更容易地定义。相比之下,古典 CppUnit (基于 JUnit )似乎采取非常保守的方法。有没有更新和更好的,使用C ++(或甚至C ++ 11)的特定力量,使生活更轻松?

What are the most modern approaches to unit testing for 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?

我一直在使用 CppUnit 框架一段时间在Windows本机C + +(Visual Studio 2005和2010)的项目的部分以相当简单的方式。我们之前没有选择测试驱动开发方法,因为已经有很多遗留代码,我们发现很难添加测试。我们不得不重构应用程序,但添加所有好的测试将是耗时的,即使在这种情况下。

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.

最近,我们已经切换到Visual Studio 2013 C ++ 11标准实现),我们将开始新的,而是长期的项目。

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.

拥有以前的良好的(小)单元测试经验,我希望尝试测试驱动开发方法。由于项目不是一个小的(预期的大小与旧的大约相同,即大约200 k行代码),我更喜欢更容易(但不是更少的能力)框架。

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.

新项目有可能导致跨平台实现(Windows和Linux)。在Visual Studio 2013中有一个单元测试支持,但我没有经验,它如何适应跨平台。

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.

到目前为止,我发现 C ++的单元测试框架列表。然而,人们不能看到它们在原理上是不同的。我目前有三个候选人(保守的选择):

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):


  • Boost - C ++标准的测试台;因此它很可能被广泛接受;可能是最大的用户组。它似乎比 CppUnit 更高级。

  • CppUnit - 我知道,但它编写所有的代码不是

  • 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.

无论如何,似乎所有的三个使用类似的方法。可能VS2013支持生成代码,但这并不意味着它会导致更简单的。

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.

有没有全新的方法?

推荐答案

唯一值得考虑的测试框架:
Catch

The only test framework worth considering: Catch

有关lib的介绍,另见此处这里

For an introduction to the lib, see also here and here

它很容易使用(一个只包含一个标题的头文件),可移植的,任何C ++单元测试框架的最简单,最干净的语法。

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.

与其他库不同,你不需要记住两个不同的宏或不同类型的断言。

And unlike other libraries, you don't need to remember two dozen different macros f or different types of assertions.

您只需使用REQUIRE:

You just use 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

使用IMO。

在我发现这之前,我曾经使用过Boost.Test,但是这是很多更复杂的设置和使用。我们在工作中使用CppUnit,这似乎被设计为尽可能脆弱和痛苦。

I used to use Boost.Test before I found this, but that was a lot more 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.

我简要地看了一下VS2013测试框架,尝试它,它看起来可容忍,但非常喜欢它模仿老守卫。它并不真的试图比CppUnit,Boost.Test和所有其他在Catch之前更清洁,更容易或更好。所以我会说不要打扰它。测试应该容易写(和理解),并且Catch比在这一面上看到的每一个其他框架都领先了。

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.

这篇关于最先进的C ++单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆