有没有办法让我的项目改用rake作为它的构建系统?我有一个使用Make作为构建系统的大型项目,并希望为单元测试添加CMock功能(我已经成功地使用Unity),但我没有找到任何关于将CMock与Makefile集成的信息(相反,如果他们想让CMock在他们的项目中工作,似乎世界已经接受了使用rake作为他们的构建系统)。
我已经能够运行CMock示例测试,其中包括一个(看似未完成?)“‘make”示例。
我已经将'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成功实现了一个项目?
发布于 2017-08-06 07:36:43
原来我是我自己的vim/clang格式配置的受害者。
如果在你的测试实现中'unity.h‘导入放在'mock_xxx.h’之前(我确信文档中的某个地方提到了),那么make_example (尽管开箱即用不是很有用)确实可以使用CMock的模拟框架正确地编译和运行测试。
下面是一个工作测试的示例(从cmock/ test_main.c /make_ example中稍微修改了一下示例):
#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',我的includes被重新排序以生成:
#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);
...我的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_LINE_TYPE是在unity.h中定义的,并注意到unity.h是如何被包含在我的测试中的,我看到它被包含在'mock_foo.h‘之后(对我来说还没有问题),然后与一些工作的rake示例(在../temp_sensor中)进行比较,我注意到所有的'unity.h’包含在所有的'mock_xxx.h‘包含之前(我还没有用vim触及这些)。
不要让你的工具变得愚蠢。
发布于 2019-03-22 17:46:56
这个错误已经被修复了:https://github.com/ThrowTheSwitch/CMock/pull/211
现在,您不需要保持#include指令的特定顺序。
发布于 2018-08-24 22:09:56
您的使用里程可能会有所不同,但我发现,使用换行符对我的包含进行分组会导致clang-format在组内进行排序,从而使我能够在开始时保留重要的标题。通常这也有一定的逻辑意义。
例如,在我的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"https://stackoverflow.com/questions/45526804
复制相似问题