簡體   English   中英

單元測試-如果在生產代碼中僅調用一次函數,則在兩個測試用例中調用函數

[英]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() 通過公共接口main_function()間接調用此函數,可以在單元測試中直接調用該接口。 有沒有人知道如何在一般或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
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM