C++单元测试框架比较

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

Comparison of C++ unit test frameworks

c++unit-testingcppunitgoogletestboost-test

提问by housemaister

I know there are already a few questions regarding recommendations for C++ unit test frameworks, but all the answers did not help as they just recommend one of the frameworks but do not provide any information about a (feature) comparison.

我知道已经有一些关于 C++ 单元测试框架建议的问题,但所有的答案都没有帮助,因为他们只推荐了其中一个框架,但没有提供任何有关(功能)比较的信息。

I think the most interesting frameworks are CppUnit, Boost and the new Google testing framework. Has anybody done any comparison yet?

我认为最有趣的框架是 CppUnit、Boost 和新的 Google 测试框架。有没有人做过比较?

采纳答案by Sam Saffron

See this questionfor some discussion.

请参阅此问题以进行一些讨论。

They recommend the articles: Exploring the C++ Unit Testing Framework Jungle, By Noel Llopis. And the more recent: C++ Test Unit Frameworks

他们推荐以下文章: 探索 C++ 单元测试框架丛林,作者:Noel Llopis。以及最近的:C++ 测试单元框架

I have not found an article that compares googletest to the other frameworks yet.

我还没有找到将 googletest 与其他框架进行比较的文章。

回答by Wernight

A new player is Google Test(also known as Google C++ Testing Framework) which is pretty nice though.

一个新的播放器是Google Test(也称为Google C++ 测试框架),虽然它非常好。

#include <gtest/gtest.h>

TEST(MyTestSuitName, MyTestCaseName) {
    int actual = 1;
    EXPECT_GT(actual, 0);
    EXPECT_EQ(1, actual) << "Should be equal to one";
}

Main features:

主要特点:

  • Portable
  • Fatal and non-fatal assertions
  • Easy assertions informative messages: ASSERT_EQ(5, Foo(i)) << " where i = " << i;
  • Google Test automaticallydetects your tests and doesn't require you to enumerate them in order to run them
  • Make it easy to extendyour assertion vocabulary
  • Death tests(see advanced guide)
  • SCOPED_TRACEfor subroutine loops
  • You can decide which tests to run
  • XMLtest report generation
  • Fixtures/ Mock/ Templates...
  • 便携的
  • 致命和非致命断言
  • 简单的断言信息性消息ASSERT_EQ(5, Foo(i)) << " where i = " << i;
  • Google Test 会自动检测您的测试,并且不需要您枚举它们来运行它们
  • 轻松扩展断言词汇
  • 死亡测试(参见高级指南)
  • SCOPED_TRACEfor 子程序循环
  • 您可以决定运行哪些测试
  • XML测试报告生成
  • 夹具/模拟/模板...

回答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. The project is hosted on Github

它还具有 Objective-C 绑定。该项目托管在Github 上

回答by Wernight

Boost Test Libraryis a very good choice especially if you're already using Boost.

Boost 测试库是一个非常好的选择,特别是如果您已经在使用 Boost。

// TODO: Include your class to test here.
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(MyTestCase)
{
    // To simplify this example test, let's suppose we'll test 'float'.
    // Some test are stupid, but all should pass.
    float x = 9.5f;

    BOOST_CHECK(x != 0.0f);
    BOOST_CHECK_EQUAL((int)x, 9);
    BOOST_CHECK_CLOSE(x, 9.5f, 0.0001f); // Checks differ no more then 0.0001%
}

It supports:

它支持:

  • Automaticor manual tests registration
  • Many assertions
  • Automatic comparison of collections
  • Various output formats (including XML)
  • Fixtures/ Templates...
  • 自动或手动测试注册
  • 许多断言
  • 自动比较集合
  • 各种输出格式(包括XML
  • 夹具/模板...

PS: I wrote an article about it that may help you getting started: C++ Unit Testing Framework: A Boost Test Tutorial

PS:我写了一篇关于它的文章,可以帮助你入门:C++ 单元测试框架:A Boost 测试教程

回答by John Deters

Wikipedia has a comprehensive list of unit testing frameworks, with tables that identify features supported or not.

Wikipedia 有一个完整的单元测试框架列表,其中包含标识支持与否的功能的表格。

回答by moswald

I've recently released xUnit++, specifically as an alternative to Google Test and the Boost Test Library (view the comparisons). If you're familiar with xUnit.Net, you're ready for xUnit++.

我最近发布了xUnit++,特别是作为 Google 测试和 Boost 测试库的替代品(查看比较)。如果您熟悉 xUnit.Net,那么您已经准备好使用 xUnit++。

#include "xUnit++/xUnit++.h"

FACT("Foo and Blah should always return the same value")
{
    Check.Equal("0", Foo()) << "Calling Foo() with no parameters should always return \"0\".";
    Assert.Equal(Foo(), Blah());
}

THEORY("Foo should return the same value it was given, converted to string", (int input, std::string expected),
    std::make_tuple(0, "0"),
    std::make_tuple(1, "1"),
    std::make_tuple(2, "2"))
{
    Assert.Equal(expected, Foo(input));
}

Main features:

主要特点:

  • Incredibly fast: tests run concurrently.
  • Portable
  • Automatictest registration
  • Manyassertion types (Boost has nothing on xUnit++)
  • Compares collectionsnatively.
  • Assertions come in threelevels:
    • fatal errors
    • non-fatal errors
    • warnings
  • Easy assertlogging: Assert.Equal(-1, foo(i)) << "Failed with i = " << i;
  • Testlogging: Log.Debug << "Starting test"; Log.Warn << "Here's a warning";
  • Fixtures
  • Data-driventests (Theories)
  • Select which tests to runbased on:
    • Attribute matching
    • Name substring matchin
    • Test Suites
  • 难以置信的快:测试同时运行。
  • 便携的
  • 自动测试注册
  • 许多断言类型(Boost 在 xUnit++ 上没有任何内容)
  • 本机比较集合
  • 断言分为三个级别:
    • 致命错误
    • 非致命错误
    • 警告
  • 简单的断言日志记录:Assert.Equal(-1, foo(i)) << "Failed with i = " << i;
  • 测试日志:Log.Debug << "Starting test"; Log.Warn << "Here's a warning";
  • 灯具
  • 数据驱动的测试(理论)
  • 选择要运行测试基于:
    • 属性匹配
    • 名称子串匹配
    • 测试套件

回答by ratkok

CppUTest- very nice, light weight framework with mock libraries. Worthwhile taking a closer look.

CppUTest- 非常好的轻量级框架,带有模拟库。值得仔细看看。

回答by Roger

CPUnit (http://cpunit.sourceforge.net) is a framework that is similar to Google Test, but which relies on less macos (asserts are functions), and where the macros are prefixed to avoid the usual macro pitfall. Tests look like:

CPUnit ( http://cpunit.sourceforge.net) 是一个类似于 Google Test 的框架,但它依赖较少的 macos(断言是函数),并且在宏前面加上前缀以避免通常的宏陷阱。测试看起来像:

#include <cpunit>

namespace MyAssetTest {
    using namespace cpunit;

    CPUNIT_FUNC(MyAssetTest, test_stuff) {
        int some_value = 42;
        assert_equals("Wrong value!", 666, some_value);
    }

    // Fixtures go as follows:
    CPUNIT_SET_UP(MyAssetTest) {
        // Setting up suite here...
        // And the same goes for tear-down.
    }

}

They auto-register, so you need not more than this. Then it is just compile and run. I find using this framework very much like using JUnit, for those who have had to spend some time programming Java. Very nice!

它们会自动注册,因此您只需要这些。然后它只是编译和运行。我发现使用这个框架非常像使用 JUnit,对于那些不得不花一些时间编写 Java 的人来说。非常好!

回答by Dave Young

There are some relevant C++ unit testing resources at http://www.progweap.com/resources.html

http://www.progweap.com/resources.html 上有一些相关的 C++ 单元测试资源

回答by linuxbuild

API Sanity Checker— test framework for C/C++ libraries:

API Sanity Checker— C/C++ 库的测试框架:

An automatic generator of basic unit tests for a shared C/C++ library. It is able to generate reasonable (in most, but unfortunately not all, cases) input data for parameters and compose simple ("sanity" or "shallow"-quality) test cases for every function in the API through the analysis of declarations in header files.

The quality of generated tests allows to check absence of critical errors in simple use cases. The tool is able to build and execute generated tests and detect crashes (segfaults), aborts, all kinds of emitted signals, non-zero program return code and program hanging.

共享 C/C++ 库的基本单元测试的自动生成器。它能够为参数生成合理的(在大多数情况下,但不幸的是不是所有情况)输入数据,并通过分析标头中的声明为 API 中的每个函数编写简单(“健全”或“浅”质量)的测试用例文件。

生成的测试的质量允许检查简单用例中是否存在严重错误。该工具能够构建和执行生成的测试并检测崩溃(段错误)、中止、各种发出的信号、非零程序返回代码和程序挂起。

Unique features in comparison with CppUnit, Boost and Google Test:

与 CppUnit、Boost 和 Google Test 相比的独特功能:

  • Automatic generation of test data and input arguments (even for complex data types)
  • Modern and highly reusable specialized typesinstead of fixtures and templates
  • 自动生成测试数据和输入参数(即使对于复杂的数据类型)
  • 现代且高度可重用的专用类型,而不是夹具和模板