1

enter image description here

Suppose that I have a module which has some static data, If I want to spy on these values using a test file, Is there an elegant way to do that without using getters?

Clifford
  • 88,407
  • 13
  • 85
  • 165
moha
  • 85
  • 1
  • 7
  • Take a look at [How to test a static function?](https://stackoverflow.com/a/593423/15168). The basic solution works for variables too. I note that the `ModuleArray` in the diagram is not 'static data' in the sense of 'qualified with the keyword `static`' so it is visible outside the module — even if there isn't (yet) a header that declares it. Of course, that could simply be a bug — the variable should be qualified with `static` unless there is a need to access it outside the module, and if it should be accessed, then there should be a header that declares it. – Jonathan Leffler Feb 14 '20 at 15:47

1 Answers1

1

Unit testing is one of the rare times it makes sense to directly include a .c file. That will give you access to static functions and variables inside the module.

For example:

#include <string.h>
#include <assert.h>

#include "file_to_test.c"

int main()
{
    Module_Init();
    assert(strcmp(ModuleArray, "string_to_test") == 0);
}
dbush
  • 205,898
  • 23
  • 218
  • 273