2

也许有人有使用cpputest进行单元测试的经验。

我有这样的事情:

被测源代码:

main_function()
{
   static int8 is_functioncalled = 1;

   if (is_functioncalled){
   my_local_function();
   is_functioncalled = 0
   }

单元测试环境:

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() 。该函数通过公共接口main_function()间接调用,该接口可在单元测试中直接调用。有没有人知道如何在一般情况下或在cpputest环境中执行此操作?

4

2 回答 2

2

尝试覆盖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 - 你需要知道的一切。

于 2017-04-05T07:45:45.913 回答
1

您可以在代码中添加一个定义,如果它正在测试中,则修改行为:

    main_function()
    {
        static int8 is_functioncalled = 1;
    #ifdef UNITTEST
        is_functioncalled = 1;
    #endif
        if (is_functioncalled){
            my_local_function();
            is_functioncalled = 0
        }
于 2017-10-12T14:05:25.080 回答