我正在使用 CUnit 框架来显示测试结果。(我是一个编程和新手,所以一步一步的回答真的很感激)。
当我测试我希望 exit() 的函数时,有什么方法可以使用相同的 CUnit 框架?对我来说似乎不是这样,但我还是很想问 - 它会显示通过/失败结果以及我的其他 CUnit 测试,所以它是理想的。
如果没有,我一直在寻找其他适合新手的解决方案(例如这个 SO post),但我不能使用 GOTO/setjmp/longjmp。该解决方案还需要便携。
我正在使用 Mac & gcc 命令行来运行此代码。
编辑 建议的解决方案之一是使用 C Pre-Processor (CPP) Directive / "mocking",这看起来很理想吗?我在 test.c 文件中使用了以下代码:
#define ERROR(PHRASE) {fprintf(stderr,"Fatal Error %s occurred in %s, line %d\n",PHRASE, FILE, LINE); exit(2);}
#ifdef ERROR(PHRASE)
#define ERROR(PHRASE) {printf("In test phase");}
#endif
#ifndef ERROR(PHRASE #define ERROR(PHRASE) {printf("Not In test phase");}
#endif
这是终端给我的错误消息:
test.c:30:9: warning: 'ERROR' macro redefined [-Wmacro-redefined]
#define ERROR(PHRASE) {printf("In test phase");}
^
test.c:26:9: note: previous definition is here
#define ERROR(PHRASE) {fprintf(stderr,"Fatal Error %s occured in %s, lin...
^
test.c:32:14: warning: extra tokens at end of #ifndef directive
[-Wextra-tokens]
#ifndef ERROR(PHRASE) {printf("Not In test phase");}
删除 (PHRASE) 仍然会产生相同的错误。
编辑 如果对其他人有帮助,使用 #ifdef 进行模拟是最终解决此问题的最简单方法。这个网站也很有帮助。