运行 使用 cgreen 从 makefile 中执行

run executable from makefile with cgreen

用 cgreen 编译单元测试使用 makefile。但是 运行 从 makefile 中明确地调用 exe 只有在通过测试时才有效。这是我的 makefile:

1
2  ## 2020-July-01
3
4  CC=gcc
5
6  .PHONY: Etarget
7  Etarget: hello
8
9  hello:
10    -$(CC) -c hello.c
11    -$(CC) hello.o  -o hello -lcgreen
12    -echo
13    -./hello

这里 hello.c:

#include <cgreen/cgreen.h>

Describe(Cgreen);
BeforeEach(Cgreen){}
AfterEach(Cgreen){}

Ensure(Cgreen, passes_this_test) {
    assert_that( 1 == 1);
}

Ensure(Cgreen, fails_this_test) {
    assert_that( 0 == 1 );
}

int main(int argc, char **argv){
    TestSuite *suite = create_test_suite();
    add_test_with_context(suite, Cgreen, passes_this_test);
    add_test_with_context(suite, Cgreen, fails_this_test);
    return run_test_suite(suite, create_text_reporter());
}

断言“fails_this_test”导致此错误:

makefile:10: recipe for target 'hello' failed
make: *** [hello] Error 1

有办法解决这个问题吗? 我只是想知道,因为我可以按顺序 运行 以下代码而不会出错。我只是无法将其打包到 makefile 中。

gcc -c hello.c
gcc hello.o -o hello -lcgreen
./hello

./hello 的输出已经充分告诉我测试结果:

Running "main" (2 tests)...
hello.c:12: Failure: fails_this_test 
    Expected [0 == 1] to [be true]

  "main": 1 pass, 1 failure in 1ms.
Completed "main": 1 pass, 1 failure in 1ms.

好吧,您实际上并没有向我们展示您的 makefile 在第 10 行的内容,所以我们只能猜测。

但是,make 有意检查您给它的命令是成功还是失败(通过查看退出代码)。例如,如果您遇到编译错误,它就是这样知道停止构建的。 Make 不知道你想让它做的事情可能会失败,除非你告诉它。

您可以通过多种方式做到这一点:一种方式是在食谱前加上 - 字符。有关详细信息,请参阅 the manual

但是,我不确定我明白你想要什么。如果您的测试失败了,为什么 不会 您希望 make 以错误退出?不然你怎么知道有什么失败了?