To have full access to all static variables and functions from inside the unittest i do include the c-file in the unittest c-file.
I use gcc-11.2.1 and the target is a small microcontroller stm32L0
Now i have same strange behaviour where i have two instances of
the static object my
and therefore also from my.var
However in this small example everything works like expected and there is only one instance of the object my
and the unittest passes with no errors, but in my much bigger development project i have debugged that the compiler uses two different instances.
Unfortunatly i cannot upload my development project so i tried to minimize the sourcecode to the relevant part for this question.
source of buildingblock.h
#ifndef __BUILDINGBLOCK_H
#define __BUILDINGBLOCK_H
#include <stdint.h>
uint8_t pub_getVar();
#endif
source of buildingblock.c
#include "buildingblock.h"
typedef struct{
uint8_t var;
}my_t;
static my_t my = {
.var = 0
};
static uint8_t getVar(){
return my.var;
}
#ifndef YES_TESTCODE
uint8_t pub_getVar(){
return my.var;
}
#endif
source of unittest.h
#ifndef __UNITTEST_H
#define __UNITTEST_H
// This define can be used to handle special handling for the test code.
// E.g. public variables/functions cannot be defined in the unit test, otherwise the linker generates an error message because of a duplicate function definition.
#define YES_TESTCODE
void ut_run();
#endif
source of unittest.c
// I use lib-unity see https://github.com/ThrowTheSwitch
#include "unity.h" // lib-unit header
#include "unittest.h"
#include "buildingblock.c"
// increment of a static variable
static void test(){
my.var++;
uint8_t ret = getVar();
TEST_ASSERT_EQUAL_UINT8(1,ret);
}
// calls this from your main
void ut_run()
{
pub_getVar();
UNITY_BEGIN();
RUN_TEST(test);
UNITY_END();
}
For my development-project i have to exclude buildingblock.c from the build
and only include the c-file. Than i have ony one instance of the object my
and i can test as expected.
Does anybody know why i have two instances in my more complex development project ? I understand that the compiler could have problems with the scope but i also get no errors or warnings but the buggy result.