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

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


当前回答

我最近发布了xUnit++,专门作为谷歌测试和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));
}

主要特点:

Incredibly fast: tests run concurrently. Portable Automatic test registration Many assertion types (Boost has nothing on xUnit++) Compares collections natively. Assertions come in three levels: fatal errors non-fatal errors warnings Easy assert logging: Assert.Equal(-1, foo(i)) << "Failed with i = " << i; Test logging: Log.Debug << "Starting test"; Log.Warn << "Here's a warning"; Fixtures Data-driven tests (Theories) Select which tests to run based on: Attribute matching Name substring matchin Test Suites

其他回答

有关讨论,请参阅这个问题。

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

我还没有找到一篇比较googletest和其他框架的文章。

Boost Test Library是一个非常好的选择,特别是如果你已经在使用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%
}

它支持:

自动或手动测试注册 很多人断言 自动比较集合 各种输出格式(包括XML) 固定装置/模板…

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

CppUTest -非常不错的轻量级框架,带有模拟库。值得一看。

CPUnit (http://cpunit.sourceforge.net)是一个类似于谷歌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.
    }

}

他们会自动注册,所以你不需要更多。然后只需编译并运行即可。我发现对于那些不得不花一些时间编写Java程序的人来说,使用这个框架非常像使用JUnit。非常好!

API完整性检查器- 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.

与CppUnit, Boost和谷歌Test相比,独特的功能:

自动生成测试数据和输入参数(即使是复杂的数据类型) 现代且高度可重用的专用类型取代了fixture和模板