Unity 框架单元测试未定义对“setUp”和“tearDown”的引用

问题描述 投票:0回答:4

我从 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.
c unit-testing gcc compiler-errors unity-test-framework
4个回答
9
投票

需要在文件中定义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. */

1
投票

单元测试框架像构造函数/析构函数一样使用这些函数。由于您正在进行简单的测试,因此您可以使用这些名称定义两个空函数。


0
投票

我也面临类似的问题。

enter image description here

解决方案:

  1. 转到您的“unity.c”文件。
  2. 搜索“void setUp(void);”

enter image description here

-> 将分号替换为括号“void setUp(void){}”。对拆解执行相同操作。


0
投票

对于任何想知道为什么 Unity 不默认提供

setUp
tearDown
弱函数定义的人。

它似乎曾经是一个功能,但由于不同编译器/编译器版本中处理弱函数的方式不一致而被删除

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

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

所以,长话短说,您需要添加:

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

到测试文件的顶部

© www.soinside.com 2019 - 2024. All rights reserved.