简体   繁体   English

使用 cgreen 从 makefile 运行可执行文件

[英]run executable from makefile with cgreen

compiling unit tests with cgreen works with a makefile.使用 cgreen 编译单元测试适用于 makefile。 But running the exe from the makefile explicitly only works when there are passing tests.但是从 makefile 显式运行 exe 仅在通过测试时才有效。 Here's my makefile:这是我的 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

and here hello.c:在这里你好。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());
}

The assert "fails_this_test" leads to this error:断言“fails_this_test”导致此错误:

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

Is there a way around this?有没有解决的办法? I'm just wondering because I can run the following code sequentially without errors.我只是想知道,因为我可以按顺序运行以下代码而不会出错。 I just can't pack it into a makefile.我只是无法将它打包成 makefile。

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

The output of./hello already sufficiently informs me of the test results: ./hello 的 output 已经充分告知我测试结果:

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.

Well, you don't actually show us what your makefile says at line 10 so we can only guess.好吧,您实际上并没有向我们展示您的 makefile 在第 10 行所说的内容,所以我们只能猜测。

However, make intentionally checks whether the command you give it succeeded or failed (by looking at the exit code).但是,make 会故意检查您给它的命令是成功还是失败(通过查看退出代码)。 That's how it knows to stop building things if you have a compile error for example.例如,如果您遇到编译错误,这就是它知道停止构建事物的方式。 Make has no idea that the thing you want it to do might be expected to fail, unless you tell it. Make 不知道你想要它做的事情可能会失败,除非你告诉它。

You can do that in various ways: one way is by prefixing the recipe with a - character.您可以通过多种方式做到这一点:一种方法是在配方前加上-字符。 See the manual for details.有关详细信息,请参阅 手册

However, I'm not sure I understand what you want.但是,我不确定我理解你想要什么。 If your test fails, why wouldn't you want make to exit with an error?如果您的测试失败,您为什么不想让 make 退出并出现错误? How else would you know that something failed?否则你怎么知道某事失败了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM