0

I'm using cpputest to perform Unit Tests of c code.

In my source code under test I have a static function which I would like to be "redirected" to a "faked" version of the function when called from the unit test environment.

Let's say, I have somethig like this:

Source Code under test:

void my_main()
{
   read(int8 address);
}

whereby;

   static int8 read(int8 address)
   {
      return something;
   }

Unit Test Environment:

TEST(MY_TESTS, READ)
{
   my_main();
}

When calling my_main() within Unit Test environment, I would like to redirect the call of

read(int8 address) to:

int8 fake_read(int8 address)
{
   //do_something
}

What will be here the correct way? I tried it already with function pointer than injection of the dependency but it does not work. Any idea?

Jens
  • 67,715
  • 15
  • 98
  • 113
JohnDoe
  • 825
  • 1
  • 13
  • 31
  • Generally, for faking or mocking a function call or any functionality mock functions are written in a separate file and are included and called from the main driver program. I am not aware of how __cpputest__ works, but if it sets any environment variable during runtime then that env var can be read in your main function and check can be made to decide which function to be called, i.e. mock function or the real function. – Gaurav Pathak Apr 03 '17 at 10:51

2 Answers2

0

Maybe you can utilize the linker to do this: Create two source files – one with the actual production code of the static function and one with the mock implementation.

For the test, link the 2nd one; and for running your application the 1st one.

ollo
  • 24,797
  • 14
  • 106
  • 155
0

Ancient question I know, but...

Look at the docs around mock_c() http://cpputest.github.io/mocking_manual.html#other_mock_support

int64_t GetTimeMS (void)
{
    mock_c ()->actualCall ("GetTimeMS");

    return (mock_c ()->returnValue ().value.longIntValue);
}
Anonymouse
  • 935
  • 9
  • 20