2

I have a C file contains some static functions, how to use google test to test those static function?

header file:

test.h
int accessData();

source file:

test.c
static int value;
static int getData()
{
   return value;
}

int accessData()
{
    if(value != 0)
    {
       return getData();
    }
    return 0;
}

static function is called by global function, but how to test those static function using google test?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
ratzip
  • 1,571
  • 7
  • 28
  • 53

3 Answers3

6

I understand that to test a function with google test, it must be visible to the test code. A static function is only visible to the current compilation unit.

A way to get around this is to "remove" the static attribute for test purposes:

#ifndef GOOGLE_TEST
# define STATIC static
#else
# define STATIC
#endif

STATIC int getData();

If this gives conflicts, for example because there aer getData() functions in many source files, you could create simple container functions:

static int getData(<arglist>);

#ifdef GOOGLE_TEST
int myModule_getData(<arglist>) {return(getData(<arglist>);}
...
#endif
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Conflicts should be unlikely if your tests exercise only one implementation file per test suite. I'd certainly advocate that as the preferable option, with the wrapper functions being the last resort. – Toby Speight Nov 27 '19 at 16:40
5

One way to achieve this is to #include the C source file into your test source (if it's using only the subset of C that's valid C++). Then, the static function is part of the same translation unit as the test code, and can be called from it:

#include "test.c"

/* here follow the tests of getData() */

The downside to this is that everything in test.c gets compiled again, with obvious impact on build times. If that gets to be a problem, you might consider extracting the static functions to be tested into their own source file (e.g. test_p.c, with the _p meaning private/internal). Then #include "test_p.c" from both test.c and your unit test.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • 1
    Additional problem with this is that Google Test compiles tests as C++. Some C code does not compile as C++. https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B – user7610 Mar 31 '23 at 18:54
  • Agreed @user7610. It's usually not hard to create C code that also compiles as C++ with the same semantics; I've added a parenthetical remark to ensure that's considered. – Toby Speight Apr 03 '23 at 09:36
  • I remember fighting with designated initializers, recently. This is a C99 feature that only got into C++ in C++20. – user7610 Apr 03 '23 at 19:07
-2

A static function has it's visibility limited to the translation unit.

AFAIK, for googletest, you need to call the function(s) under test from a separate test file conating the test code written with TEST(). If the function under test is static, simply it is not possible to test that directly from the TEST() MACRO invocation.

The straightway approach is to, comment out the static modifier for the function for testing purpose.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Considering that usual projects have tens if not even hundrets or thousands of compilation units, commenting out the `static` keyword isn't a very pratical approach. – Anticom Sep 07 '17 at 16:14