4

我正在使用 CUnit 进行项目单元测试。我需要测试我是否使用正确的参数调用 libc 函数以及我是否以正确的方式处理它们的返回值。例如:如果我调用 bind(...) 函数 - 我想检查我传递了哪个 af 参数并断言这是错误的参数,如果我检查它我想模拟它的返回值和断言正确的方式。

出于这些目的,我希望 CUnit 环境有一个内置机制,让我在测试时调用“模拟”bind() 函数,在运行代码时调用真正的 bind() 函数 - 但我找不到类似的东西这。

你能告诉我我是否在 CUnit 中遗漏了一些东西,或者建议一种方法来实现它。

谢谢,乔。

4

2 回答 2

5

不幸的是,你不能用 CUnit 模拟 C 中的函数。

但是您可以通过使用和滥用定义来实现自己的模拟函数:假设您在编译测试时定义了 UNITTEST,您可以在测试文件(或包含)中定义如下内容:

#ifdef UNITTEST
    #define bind mock_bind
#endif

在您将在测试模式下编译的 mock_helper.c 文件中:

static int mock_bind_return;    // maybe a more complete struct would be usefull here
static int mock_bind_sockfd;

int mock_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
    CU_ASSERT_EQUAL(sockfd, mock_bind_sockfd);
    return mock_bind_return;
}

然后,在您的测试文件中:

extern int mock_bind_return;
extern int mock_bind_sockfd;

void test_function_with_bind(void)
{

   mock_bind_return = 0;
   mock_bind_sockfd = 5;
   function_using_bind(mock_bind_sockfd);
}
于 2011-12-01T09:48:29.983 回答
0

glibcmock是使用Google Test模拟 libc 函数的解决方案。例如:

#include "got_hook.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <sys/socket.h>

#include <mutex>
#include <memory>

struct MockBind {
    MOCK_METHOD3(Bind, int(int, const struct sockaddr*, socklen_t));
};

static MockBind *g_mock{nullptr};

static int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
    return g_mock->Bind(sockfd, addr, addrlen);
}

static std::mutex g_test_mutex;

TEST(BindTest, MockSample) {
    std::lock_guard<std::mutex> lock(g_test_mutex);
    std::unique_ptr<MockBind> mock(g_mock = new MockBind());
    testing::GotHook got_hook;
    ASSERT_NO_FATAL_FAILURE(got_hook.MockFunction("bind", (void*)&Bind));
    // ... do your test here, for example:
    struct sockaddr* addr = nullptr;
    EXPECT_CALL(*g_mock, Bind(1, addr, 20)).WillOnce(testing::Return(0));
    EXPECT_EQ(0, bind(1, addr, 20));
}
于 2017-03-12T02:30:22.160 回答