I am learning unit testing. I can compile simply programs with Eclipse and CppUTEST. It works. Howover I have problem to understaned how to override/stub headers files. I came up with a test scenario,
My files:
/src:
---------- src.c src.h //native compilator, for example stm-gcc
/test:
---------- AllTests.cpp Test_src.cpp //test compilator, for example gcc
/mocks:
---------- fsrc.h fsrc.c //fake file, should override src.h
Theoretically, in file "src.h" are stored settings for native compiller (stm-gcc). My test compiler (gcc) cannot use these dependencies, so I should override/stub this header file. That is why I create /mocks/fsrc.h file. fsrc.h should override/stub src.h file.
Example files code:
AllTests:
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTestExt/MockSupport.h"
#include <src/src.h> // -> should read the "fsrc" file instead "src"
int main(int ac, char** av) { return CommandLineTestRunner::RunAllTests(ac, av); }
Test_src.cpp
#include "CppUTest/TestHarness.h"
#include <src.h>
class ClassName { int a; };
TEST_GROUP(ClassName)
{
ClassName* className;
void setup() { className = new ClassName(); }
void teardown() { delete className; }
};
TEST(ClassName, Create)
{
CHECK(0 != className);
STRCMP_EQUAL(myString, "my_string_from_mock_header"); // --> myString macro should be read from "mocks/fsrc" not from "src/src.h"
}
mocks/fsrc.h
#ifndef SRC_SRC_H_
#define SRC_SRC_H_
#define myString "mocks/Header"
int fun();
#endif /* SRC_SRC_H_ */
mocks/fsrc.c
#include "fsrc.h"
int fun() { return 999; }
src/src.h
#ifndef SRC_SRC_H_
#define SRC_SRC_H_
#define myString "src/Header"
int fun();
#endif /* SRC_SRC_H_ */
src/src.c
#include "src.h"
int fun() { return 1; }
Eclipse erorr:
'g++ -I"C:\cygwin64\home\karb\cpputest\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"AllTests.d" -MT"AllTests.o" -o "AllTests.o" "../AllTests.cpp" ../AllTests.cpp:5:10: fatal error: src/src.h: No such file or directory'
When i change:
include <src/src.h> to include "src/src.h"
compilation works, but then overwriting header files does not work. What am I doing wrong that I can't overwrite the main header file?