Unity Framework Unit Testing undefined reference to "setUp" and "tearDown"

4.2k Views Asked by At

I copied the simplest code from the Unity for C Unit Testing library and set up a very basic test, which I copied and pasted from the site:

#include "unity/src/unity.h"

int main(void)
{
    UNITY_BEGIN();
    int a = 1;
    TEST_ASSERT( a == 1 ); //this one will pass
    TEST_ASSERT( a == 2 ); //this one will fail
    return UNITY_END();
}

What I typed into terminal:

gcc TestDumbExample.c ./unity/src/unity.c -o Test

I am getting this error message in Terminal:

/tmp/ccqgFGn8.o: In function `UnityDefaultTestRun':
unity.c:(.text+0x26af): undefined reference to `setUp'
unity.c:(.text+0x26ca): undefined reference to `tearDown' collect2:
error: ld returned 1 exit status

Unsure why this error is occurring and it has undefined references.
3

There are 3 best solutions below

0
On

I face a similar issue.

enter image description here

Solution:

  1. Go to your "unity.c" file.
  2. Search for "void setUp(void);"

enter image description here

-> Replace the semicolon with parenthesis "void setUp(void){}". Do the same for tearDown.

0
On

Need to define setUp and tearDown in the file, i thought it was in unity.c

void setUp (void) {} /* Is run before every test, put unit init calls here. */
void tearDown (void) {} /* Is run after every test, put unit clean-up calls here. */
0
On

The Unit Test Framework uses these functions like a constructor/destructor. Since you are doing a simple test you can just define two empty functions with these names.