2

maybe there is someone who has experience with Unit Testing with cpputest.

I have something like this:

Source code Under Test:

main_function()
{
   static int8 is_functioncalled = 1;

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

UNIT Test Environment:

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

}

I need in TEST_CASE2 the function my_local_function() to be called again. This function is indirectly called through the public interface main_function() which can be called directly within the Unit Test. Does anybody have an idea how to do this in generally or in cpputest environment ?

JohnDoe
  • 825
  • 1
  • 13
  • 31

2 Answers2

2

Try to override setup() method of test group - it will be called before each test. You could reset is_functioncalled flag there if you would put it in global scope, something like this:

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;
   }
}

Try https://cpputest.github.io/manual.html - there's all you need to know.

Kamil Flaga
  • 106
  • 4
  • Thanks for the tip. The problem is that I can not put it to global scope because of some policies. – JohnDoe Apr 05 '17 at 07:50
  • @kingking Well, as far as I know there's no way to access static function variable outside of function and is_functioncalled flag needs to be accessible from tests to reset it. Maybe chaining main_function() signature to main_function(int8 reset = 0) would do? reset != 0 would be passed for special cases (like tests) only and set flag to 1. – Kamil Flaga Apr 05 '17 at 08:03
1

You may add a define into your code, modifying behaviour if it is under testing:

    main_function()
    {
        static int8 is_functioncalled = 1;
    #ifdef UNITTEST
        is_functioncalled = 1;
    #endif
        if (is_functioncalled){
            my_local_function();
            is_functioncalled = 0
        }