如何将CMock单元测试框架与Make集成

时间:2017-08-05 22:11:00

标签: c unit-testing unity3d makefile cmock

是否有办法将我的项目切换为使用rake作为其构建系统?我有一个大型项目使用Make作为其构建系统,并希望为单元测试添加CMock功能(我已经成功使用Unity),但没有找到有关将CMock与Makefile集成的信息(相反,它似乎世界上只有如果他们希望CMock使用他们的项目,可以使用Ruby的rake作为他们的构建系统。)

我能够运行CMock示例测试,其中包括一个(看似未完成的?)'make'示例。

我已将'make_example'中的测试代码修改为以下内容:

i

但是,当使用Makefile从文件夹运行'make'时,似乎UNITY_LINE_TYPE未定义(例如我的依赖链中缺少某些内容):

#include "mock_foo.h"
#include "unity.h"

void setUp(void) {}

void tearDown(void) {}

void test_main_should_initialize_foo(void) {
        foo_init_Ignore();
        TEST_IGNORE_MESSAGE("TODO: Implement main!");
}

有没有人使用Makefiles和CMock成功实现了一个项目?

3 个答案:

答案 0 :(得分:1)

原来我是我自己的一些vim / clang格式配置的受害者。

make_example(尽管开箱即用并不是非常有用)使用CMock的模拟框架进行编译和运行测试,如果< unity.h'导入之前 xxx .h'在你的测试实现中(我确定在某处的文档中调用了它)。

以下是工作测试的示例(来自cmock / examples / make_example的稍微修改过的test_main.c):

#include "unity.h"
#include "mock_foo.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_main_should_initialize_foo(void)
{
    foo_init_Expect();
    foo_init();
}

但是,由于我的vim / clang格式设置为' SortIncludes:true',我的包含被重新排序以生成:

#include "mock_foo.h" // <---- the mocks must be included *after* unity.h
#include "unity.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_main_should_initialize_foo(void)
{
    foo_init_Expect();
    foo_init();
}

就像我一样:wim in vim。

这当然会导致CMock问题,因为它需要在尝试生成mock_foo.h之前找到的unity.h定义,所以我收到了上面发布的错误:

...
In file included from test/test_main.c:1:0:
./build/test/mocks/mock_foo.h:27:27: error: unknown type name ‘UNITY_LINE_TYPE’
 void foo_init_CMockExpect(UNITY_LINE_TYPE cmock_line);
...

我的Makefile(未触及CMock示例),后代:

CC ?= gcc
BUILD_DIR ?= ./build
SRC_DIR ?= ./src
TEST_DIR ?= ./test
TEST_BUILD_DIR ?= ${BUILD_DIR}/test
TEST_MAKEFILE = ${TEST_BUILD_DIR}/MakefileTestSupport
OBJ ?= ${BUILD_DIR}/obj
OBJ_DIR = ${OBJ}

default: all

all: setup test ${BUILD_DIR}/main run

setup:
    mkdir -p ${BUILD_DIR}
    mkdir -p ${OBJ}
    ruby ../../scripts/create_makefile.rb --silent

clean:
    rm -rf ${BUILD_DIR}

${BUILD_DIR}/main: ${SRC_DIR}/main.c ${SRC_DIR}/foo.c
    ${CC} $< -o $@

run:
    ./build/main || true

test: setup

-include ${TEST_MAKEFILE}

我发现发生了这种情况,发现在unity.h中定义了UNITY_LINE_TYPE并注意了我的测试中如何包含unity.h,看到&#39;之后mock_foo.h&#39; (对我来说没问题),然后比较一些工作的rake例子(在../temp_sensor中),我注意到所有的&#39; unity.h&#39;包括在所有&#39; mock_ xxx .h&#39;之前。包括(我还没有用vim触及这些。)

不要让你的工具变得愚蠢。

答案 1 :(得分:0)

您的里程可能会有所不同,但是我发现将包含的内容与换行符进行分组会导致clang格式在组内进行排序,从而使我可以在开头保留重要的标头。通常,这也具有一定的逻辑意义。

例如,在我的test_xyz.c文件中:

#include "unity.h"

#include "support/logging.h"
#include "crc.h"
#include "mock_unistd.h"

排序为:

#include "unity.h"

#include "crc.h"
#include "mock_unistd.h"
#include "support/logging.h"

答案 2 :(得分:0)

此错误已得到修复:https://github.com/ThrowTheSwitch/CMock/pull/211

现在,您无需保留#include指令的特定顺序。