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



有没有办法将我的项目切换为使用 rake 作为其构建系统?我有一个使用 Make 作为构建系统的大型项目,并希望添加 CMock 功能进行单元测试(我已经成功使用 Unity(,但没有找到有关将 CMock 与 Makefiles 集成的信息(相反,如果他们希望 CMock 与他们的项目一起工作,似乎世界刚刚接受了使用 Ruby 的 rake 作为他们的构建系统(。

我已经能够运行 CMock 示例测试,其中包括一个(似乎未完成?"制作"示例。

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

#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!");
}

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

make
mkdir -p ./build
mkdir -p ./build/obj
ruby ../../scripts/create_makefile.rb --silent
cc -o build/test/obj/test_main.o -c test/test_main.c -DTEST -I ./src -I /home/user/Documents/gitrepos/cmock/vendor/unity/src -I /home/user/Documents/gitrepos/cmock/src -I ./build/test/mocks 
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);
^
build/test/MakefileTestSupport:29: recipe for target 'build/test/obj/test_main.o' failed
make: *** [build/test/obj/test_main.o] Error 1

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

事实证明,我是我自己的一些vim/clang格式配置的受害者。

如果将"unity.h"导入放在测试实现中的">mock_xxx.h">之前则该make_example(尽管开箱即用不是很有用(确实可以使用CMock的模拟框架正确编译和运行测试(我确信在文档中某处调用了(。

以下是工作测试的示例(从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 格式设置为"SortInclude: 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();
}

就像我在 vim 中的 :wq 一样。

当然,这会导致 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);
...

我的制作文件(未触及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 如何包含在我的测试中,看到它包含在"mock_foo.h">之后(对我来说还没有明显的问题(,然后与一些工作耙子示例进行比较(在 ../temp_sensor( 我注意到所有包含的"unity.h"都在所有包含的">mock_xxx.h"之前(我还没有用 vim 接触过这些。

不要让你的工具成为傻瓜。

这个错误刚刚得到修复:https://github.com/ThrowTheSwitch/CMock/pull/211

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

您的里程可能会有所不同,但我发现使用换行符对我的包含进行分组会导致 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"

最新更新