我从 Unity for C 单元测试库复制了最简单的代码,并设置了一个非常基本的测试,我从网站复制并粘贴了该测试:
#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();
}
我在终端中输入的内容:
gcc TestDumbExample.c ./unity/src/unity.c -o Test
我在终端中收到此错误消息:
/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.
需要在文件中定义setUp和tearDown,我以为是在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. */
单元测试框架像构造函数/析构函数一样使用这些函数。由于您正在进行简单的测试,因此您可以使用这些名称定义两个空函数。
对于任何想知道为什么 Unity 不默认提供
setUp
和 tearDown
弱函数定义的人。
它似乎曾经是一个功能,但由于不同编译器/编译器版本中处理弱函数的方式不一致而被删除
https://github.com/ThrowTheSwitch/Unity/issues/438#issuecomment-544452555
所以,长话短说,您需要添加:
void setUp(void) {};
void tearDown(void) {};
到测试文件的顶部