我知道已经有一些关于推荐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

其他回答

我最近发布了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和其他框架的文章。

Wikipedia提供了一个完整的单元测试框架列表,并列出了支持或不支持的特性。

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和模板

一个新的播放器是谷歌测试(也称为谷歌c++测试框架),这是非常好的。

#include <gtest/gtest.h>

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

主要特点:

可移植的 致命和非致命断言 ASSERT_EQ(5, Foo(i)) << " where i = " << i; 谷歌Test自动检测您的测试,不需要您枚举它们以运行它们 使扩展断言词汇表变得容易 死亡试验(见高级指南) 子例程循环的SCOPED_TRACE 您可以决定运行哪些测试 生成XML测试报告 fixture / Mock /模板…