最新的c++单元测试?

时间:2021-08-29 12:09:24

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?

对于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 native - 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.

有了以前好的(小的)单元测试经验,我想尝试测试驱动的开发方法。由于该项目不是一个小项目(预期大小与旧项目相同,即大约200k行代码),所以我更喜欢更简单(但功能也不差)的框架。

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.
  • Boost——可能的候选者;试验台为c++标准;因此,它很可能被广泛接受;可能是最大的用户群。它似乎比CppUnit更先进。
  • CppUnit -- I know it, but it writing all of the code around is not a pleasure.
  • CppUnit,我知道,但是它把所有的代码都写出来并不是一种乐趣。
  • Visual Studio 2013 built-in -- new to me, somehow can generate the skeletons probably.
  • 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?

有什么全新的方法吗?

2 个解决方案

#1


48  

The only test framework worth considering: Catch

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

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

关于lib的介绍,请参见这里和这里。

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.

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

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

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

我以前用Boost。在我发现这个之前测试,但是设置和使用起来要麻烦得多。我们在工作中使用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更简洁、更简单或更好。测试和所有其他的在捕获之前。所以我得说,别麻烦了。测试应该很容易编写(也很容易理解),而Catch比我在前面看到的所有其他框架都早了好几光年。

#2


4  

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的项目,那么我完全推荐它。

#1


48  

The only test framework worth considering: Catch

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

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

关于lib的介绍,请参见这里和这里。

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.

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

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

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

我以前用Boost。在我发现这个之前测试,但是设置和使用起来要麻烦得多。我们在工作中使用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更简洁、更简单或更好。测试和所有其他的在捕获之前。所以我得说,别麻烦了。测试应该很容易编写(也很容易理解),而Catch比我在前面看到的所有其他框架都早了好几光年。

#2


4  

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的项目,那么我完全推荐它。