单元测试 - 如果函数在生产代码中只被调用一次,则在两个测试用例中调用一个函数
Unit Test - call a function in two test cases if the function is called only once in productive code
也许有人有使用 cpputest 进行单元测试的经验。
我有这样的东西:
正在测试的源代码:
main_function()
{
static int8 is_functioncalled = 1;
if (is_functioncalled){
my_local_function();
is_functioncalled = 0
}
UNIT 测试环境:
TEST(TESTGROUP,TEST_CASE1){
//Some Unit Test checks of my_local_function()
main_function();
}
TEST(TESTGROUP,TEST_CASE2){
//Some other Unit Test stuff
main_function(); // --> my_local_function() will not be called in this test case because it's called already before
}
我需要在 TEST_CASE2 中再次调用函数 my_local_function()。该函数是通过public接口main_function()间接调用的,在Unit Test中可以直接调用。有没有人知道如何在一般情况下或在 cpputest 环境中执行此操作?
尝试覆盖测试组的 setup()
方法 - 它将在每次测试之前调用。如果你想把它放在全局范围内,你可以在那里重置 is_functioncalled
标志,像这样:
static int8 is_functioncalled = 1;
main_function()
{
if (is_functioncalled){
my_local_function();
is_functioncalled = 0
}
}
//
extern int8 is_functioncalled; // If its in global scope in other source file
TEST_GROUP(TESTGROUP)
{
void setup()
{
is_functioncalled = 1;
}
}
试试 https://cpputest.github.io/manual.html - 你需要知道的都有了。
您可以在代码中添加一个定义,修改正在测试中的行为:
main_function()
{
static int8 is_functioncalled = 1;
#ifdef UNITTEST
is_functioncalled = 1;
#endif
if (is_functioncalled){
my_local_function();
is_functioncalled = 0
}
也许有人有使用 cpputest 进行单元测试的经验。
我有这样的东西:
正在测试的源代码:
main_function()
{
static int8 is_functioncalled = 1;
if (is_functioncalled){
my_local_function();
is_functioncalled = 0
}
UNIT 测试环境:
TEST(TESTGROUP,TEST_CASE1){
//Some Unit Test checks of my_local_function()
main_function();
}
TEST(TESTGROUP,TEST_CASE2){
//Some other Unit Test stuff
main_function(); // --> my_local_function() will not be called in this test case because it's called already before
}
我需要在 TEST_CASE2 中再次调用函数 my_local_function()。该函数是通过public接口main_function()间接调用的,在Unit Test中可以直接调用。有没有人知道如何在一般情况下或在 cpputest 环境中执行此操作?
尝试覆盖测试组的 setup()
方法 - 它将在每次测试之前调用。如果你想把它放在全局范围内,你可以在那里重置 is_functioncalled
标志,像这样:
static int8 is_functioncalled = 1;
main_function()
{
if (is_functioncalled){
my_local_function();
is_functioncalled = 0
}
}
//
extern int8 is_functioncalled; // If its in global scope in other source file
TEST_GROUP(TESTGROUP)
{
void setup()
{
is_functioncalled = 1;
}
}
试试 https://cpputest.github.io/manual.html - 你需要知道的都有了。
您可以在代码中添加一个定义,修改正在测试中的行为:
main_function()
{
static int8 is_functioncalled = 1;
#ifdef UNITTEST
is_functioncalled = 1;
#endif
if (is_functioncalled){
my_local_function();
is_functioncalled = 0
}