ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
4.02k stars 969 forks source link

How do I reset state of static variables inside functions called by multiple tests? #699

Closed AgainPsychoX closed 11 months ago

AgainPsychoX commented 1 year ago

Let's say I have following:

test/TestProductionCode.c

#include "ProductionCode.h"
#include "unity.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_foo_shouldEqualOne_A(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

void test_foo_shouldEqualOne_B(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

src/ProductionCode.c

#include <stdio.h>
#include "ProductionCode.h"

int foo()
{
    static int x = 0;
    x++;
    printf("x=%u\n", x);
    return x;
}

When I compile and run the test I get:

./test1.out
x=1
test/TestProductionCode.c:13:test_foo_shouldEqualOne_A:PASS
x=2
test/TestProductionCode.c:20:test_foo_shouldEqualOne_B:FAIL: Expected 1 Was 2

-----------------------
2 Tests 1 Failures 0 Ignored 
FAIL

One of test cases will always fail, as the tests are compiled into one binary, therefore sharing the static variable between test cases execution. How do I prevent that?

In some other languages/testing frameworks there is a way to mark test to be run in other process. As far I know there is no way to access the static variable in function, but I would be happy enough if I could reset the variable even by some undefined behavior magic...

Of course while "just don't use static variables"/don't make side-effects might apply, it's not my code.

mvandervoord commented 1 year ago

A trick is similar to how we tell people to use STATIC:

#ifdef TEST
#define STATIC
#else
#define STATIC static
#endif

This can be used to give test access to static variables, like so:

STATIC int privateInteger = 0;

Your case is similar, except we're going to basically move the variable out of the function for testing only:

#include <stdio.h>
#include "ProductionCode.h"

#ifdef TEST
int x = 0;
#endif

int foo()
{
    #ifndef TEST
    static int x = 0;
    #endif
    x++;
    printf("x=%u\n", x);
    return x;
}

You now have access to directly effect x:

#include "ProductionCode.h"
#include "unity.h"

extern int x;

void setUp(void)
{
   x = 0;
}

void tearDown(void)
{
}

void test_foo_shouldEqualOne_A(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

void test_foo_shouldEqualOne_B(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}
AgainPsychoX commented 1 year ago

Thanks!

I found similar way: exposing pointer to the variable to global variable (valid after function runs first).

test/TestProductionCode.c

#include "ProductionCode.h"
#include "unity.h"

extern int* foo_static_x;

void setUp(void)
{
    if (foo_static_x != NULL) 
    {
        *foo_static_x = 0;
    }
}

void tearDown(void)
{
}

void test_foo_shouldEqualOne_A(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

void test_foo_shouldEqualOne_B(void)
{
    TEST_ASSERT_EQUAL(1, foo());
}

src/ProductionCode.c

#include <stdio.h>
#include "ProductionCode.h"

#ifdef UNIT_TESTS
int* foo_static_x = NULL;
#endif

int foo(void)
{
    static int x = 0;
    #ifdef UNIT_TESTS
    foo_static_x = &x;
    #endif
    x++;
    printf("x=%u\n", x);
    return x;
}

It works:

./test1.out
x=1
test/TestProductionCode.c:19:test_foo_shouldEqualOne_A:PASS
x=1
test/TestProductionCode.c:24:test_foo_shouldEqualOne_B:PASS

Could you share your opinion @mvandervoord ?

I guess there is a small disadvantage of this approach: not being able to set the variable to custom value for the very first test - but as for resetting it's perfectly fine. Advantage is I can have multiple functions with multiple static variables inside of the same name.

mvandervoord commented 1 year ago

That option makes sense to me.

Also, with your version, your first test could actually choose to verify that the pointer isn't null AND that it's set to exactly 1 after calling it the function for the first time.