[英]How do you mock and test the same function in C/C++ with FFF and Google Test?
我正在探索 TDD(测试驱动开发)来测试我在 C 和 C++ 中编写的代码。 我选择使用 Google Test 作为单元测试框架。 我选择使用 FFF 作为 mocking 框架。
我写了一些测试并运行它们,效果很好。 但是我遇到了一个问题,我无法在网上找到任何参考资料,我希望社区可以帮助我(这也可以帮助其他人)。
我遇到的问题是我想为 function A1 编写一个测试(参见下面的场景 1)。 由于它调用了另外三个函数(B1、B2 和 B3)并且它们有很多依赖关系,因此我决定模拟它们,以便测试可能影响 function A1 行为的各种场景。 为了使模拟工作并避免 linker 错误(例如“B1 的多个定义”),我需要在函数(B1、B2 和 B3)之前编写“属性((弱))”。 到目前为止,一切都很好。 一切都很好。
现在,考虑下面的场景 2。 在这种情况下,我想在单独的测试中测试 function B1。 同样,我也会模拟它调用的函数(C1、C2、C3)。 但是,问题是我不能调用“真实的”B1 function,因为如果我这样做,我将得到我之前在 A1 测试中定义的模拟 function(场景 94F41A 下定义) ZC1C425268E68385D1AB5074C1
那么在这种情况下我该怎么办呢? 谢谢。
在 James Grenning 所著的《嵌入式 C 的测试驱动开发》一书中,我做了一些更多的挖掘和发掘,至少有两个解决这个问题的方法。
此选项的一般摘要:
总体而言,这似乎不太直观,难以实现和遵循,并且需要一些学习曲线。 但是,好处是您不需要更改生产代码中的任何内容,这非常重要。
在这种情况下,您需要使用 makefile 来执行以下步骤:
将您的生产代码构建到库中
请务必将您的测试分成不同的文件,以便需要使用某个 function 作为模拟的测试与需要使用相同 function 的原始实现的测试分开。
使用 make 文件,您将需要对部分代码进行微构建,最后将它们组合在一起。 例如,对于特定的 function,您希望在不同的测试中模拟和使用原始实现,这些测试被分隔在两个文件中(test1.cpp 包含 func A1 的模拟实现,test2.cpp 包含function A1) 你会做:
现在这三个步骤只是一个高级解释。 我知道这并不理想,但仍然值得。 我承认我不是自己做的,但我确实读过 James Grenning 的书,如果您愿意,他会在他的书中的附录 1(标题为开发系统测试环境)中更详细地解释它,您可能会看到 makefile 结构他在这里使用了他的书代码示例: https://pragprog.com/titles/jgade/test-driven-development-for-embedded-c/
此选项的一般摘要:
这更加直观且易于实现。 但是,缺点是它需要对您的生产代码进行细微的更改,在该代码中声明和定义了 function。
假设您要模拟一个名为 A1 的 function 文件 Production.c 文件中定义为:
//Production.c
int A1(void)
{
... original implementation written here
}
在 Production.h 中声明为:
//Production.h
int A1(void);
因此您可以通过这种方式更改 function 声明和定义:
//Production.c
int A1_original(void)
{
... original implementation written here
}
int (*A1)(void) = A1_original;
在 Production.h 中声明为:
//Production.h
extern int (*A1)(void);
#ifdef TDD_ENABLED // use ifdef with TDD_ENABLED which is defined only in unit test project. This is because you want to declare the original implementation function as a public function so that you can freely assign it to the function pointer A1 in the test files.
int A1_original(void);
#endif
现在,对于要使用原始 function 实现的每个测试,只需按照更改前的相同方式调用它:
A1();
如您所见,这意味着,在整个生产代码中,您无需更改调用 function 的方式。 这也适用于您的测试文件。
现在,如果您想为此 function 使用模拟,您只需执行以下操作:
//Test1.cpp
int Fake_A1(void)
{
... fake function implementation
}
TEST(test_group_name,test_name)
{
int (*temp_holder)(void) = A1; // hold the original pointer in a temp pointer
A1 = Fake_A1; // assign A1 to call the mock function
... run all the test here
A1 = temp_holder; // assign A1 to call the original function back again so that the mock function is used only in the scope of this test
}
理想情况下,如果您打算进行多个此类测试,您可以在使用带有Setup()
和 Teardown( Teardown()
的 class 并使用如下测试夹具 ( TEST_F
) 时对模拟进行分配并重新分配给原始 function:
//Test1.cpp
class A1_Func_Test : public ::testing::Test
{
protected:
int (*temp_holder)(void) = A1; // hold the original pointer in a temp pointer
virtual void SetUp()
{
A1 = Fake_A1; // assign A1 to call the mock function
}
virtual void TearDown()
{
A1 = temp_holder; // assign A1 to call the original function back again so that the mock function exists only in the scope of this test
}
};
TEST_F(A1_Func_Test , Test1_A1)
{
write tests here...
}
TEST_F(A1_Func_Test , Test2_A1)
{
write tests here...
}
按照上面编写的说明进行操作后,应对您的生产代码文件(Production.c 和 Production.h)进行相同的更改。 但是,对于您的单元测试文件,您只需执行以下操作,以防您想模拟 function (如果您想在没有 mocking 的情况下测试 function )然后定期调用它:
//Test1.cpp
//Using FFF Mocking framework:
DEFINE_FFF_GLOBALS;
FAKE_VALUE_FUNC(int, A1_mock);
int A1_custom(void)
{
write code here for mock function implementation...
}
TEST(test_group_name,test_name)
{
int (*temp_holder)(void) = A1; // hold the original pointer in a temp pointer
// setting customized mock function for this test
A1_mock_fake.custom_fake = A1_custom;
A1 = A1_mock;
// simple example of a test using the the FFF framework:
int x;
x = A1();
ASSERT_EQ(A1_mock_fake.call_count, 1);
// assign A1 to call the original function back again so that the mock function exists only in the scope of this test
A1 = temp_holder;
RESET_FAKE(A1_mock); // reset all parameters of the mock function used so when used in a subsequent test we will start "clean"
}
我相信这回答了关于如何 go 的问题并按照我的要求进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.