Unity Framework Unit Testing undefined reference to "setUp" and "tearDown"
Asked Answered
L

4

9

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.
Lolalolande answered 20/8, 2019 at 12:39 Comment(0)
L
9

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. */
Lolalolande answered 20/8, 2019 at 12:53 Comment(0)
L
1

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.

Lizzielizzy answered 20/8, 2019 at 12:54 Comment(0)
B
1

For anyone wondering why Unity doesn't provide weak functions definition for setUp and tearDown as a default.

It seems like it used to be a feature, but was removed because of inconsistencies around how weak functions are handled in different compilers / compiler versions

https://github.com/ThrowTheSwitch/Unity/issues/438#issuecomment-544452555

https://github.com/ThrowTheSwitch/Unity/pull/453/files#diff-c0f259e6ee3e1cf66f0ec45cd3c2745b6001c72157bfdcfca73f9c7f18f1f763

So yeh, long story short you need to add:

void setUp(void) {};
void tearDown(void) {};

To the top of your test file

Blenny answered 9/5 at 15:22 Comment(0)
P
0

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.

Pahoehoe answered 23/5, 2021 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.