1

如何使用 ThrowTheSwitch 的 Unity C 单元测试库?

我正在尝试使用ThrowTheSwitch 的 Unity 库在 C 中创建我的单元测试,但我遇到了一些奇怪的错误,但我是新手,所以这可能是一些基本的东西。

这是我的代码:这是mytest.c

int testValue = 45;

int testThisPass(int idx)
{
    if idx == 42
        return 1;
    
    return 0;
}

int testThisNotPass(int idx)
{
    return 42;
} 

这是mytest.h

int testThisPass(int idx);
int testThisNotPass(int idx);

这是mytestMain.c

#include "unity.h"
#include "unity_internals.h"
#include "mytest.h"
#include <stdio.h>

void setUp(void) {
    //testValue = 40;
}

void tearDown(void) {
    // clean stuff up here
}

void test_function_should_doBlahAndBlah(void) {
    TEST_ASSERT_EQUAL(1,testThisPass(42));
}

void test_function_should_doAlsoDoBlah(void) {
    TEST_ASSERT_EQUAL(42, testThisNotPass(5));
}

// not needed when using generate_test_runner.rb
int main(void) {
    UNITY_BEGIN();
    RUN_TEST(test_function_should_doBlahAndBlah);
    RUN_TEST(test_function_should_doAlsoDoBlah);
    return UNITY_END();
}

这是我尝试编译时遇到的错误mytestMain.c

/usr/bin/ld: /tmp/ccOOjz1w.o: in function `test_function_should_doBlahAndBlah':
mytestMain.c:(.text+0x24): undefined reference to `testThisPass'
/usr/bin/ld: mytestMain.c:(.text+0x43): undefined reference to `UnityAssertEqualNumber'
/usr/bin/ld: /tmp/ccOOjz1w.o: in function `test_function_should_doAlsoDoBlah':
mytestMain.c:(.text+0x58): undefined reference to `testThisNotPass'
/usr/bin/ld: mytestMain.c:(.text+0x77): undefined reference to `UnityAssertEqualNumber'
/usr/bin/ld: /tmp/ccOOjz1w.o: in function `main':
mytestMain.c:(.text+0x8e): undefined reference to `UnityBegin'
/usr/bin/ld: mytestMain.c:(.text+0xa6): undefined reference to `UnityDefaultTestRun'
/usr/bin/ld: mytestMain.c:(.text+0xbe): undefined reference to `UnityDefaultTestRun'
/usr/bin/ld: mytestMain.c:(.text+0xc3): undefined reference to `UnityEnd'
collect2: error: ld returned 1 exit status

我应该怎么做才能工作?我在这里错过了什么?

4

1 回答 1

1

The problem was that I linked in the unity with them. when I include it, it works.

于 2021-10-03T11:43:12.530 回答