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.
4

3 回答 3

5

需要在文件中定义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. */
于 2019-08-20T12:53:05.353 回答
1

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

于 2019-08-20T12:54:37.283 回答
0

我面临类似的问题。

在此处输入图像描述

解决方案:

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

在此处输入图像描述

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

于 2021-05-23T01:02:08.667 回答