【问题标题】:Testing using command line with makefile [duplicate]使用带有makefile的命令行进行测试[重复]
【发布时间】:2021-07-05 13:28:09
【问题描述】:

我写了一个 C 代码。我想使用 makefile 对其进行测试,因为代码主要围绕命令行参数工作并返回退出代码,printf() 写出原因。 return 0; //SUCCESSreturn 1; //FALIURE

我使用this 文章作为参考指南,以达到我的预期目的。

这是一个想法的结构。

MAKE = gcc
FILENAME = topcgpas.c
TARGET = topcgpas

compile:
    $(MAKE) $(FILENAME) -o $(TARGET)

run: compile
    ./$(TARGET) students.csv a.csv

testAll: compile test1 test2 test3 test4 test5 test6
    @echo "All done"

test1:
    @echo "TEST: Incorrect number of arguments"
    ./$(TARGET) 1 2 3

test2:
    @echo "TEST: Incorrect input/output filename"
    ./$(TARGET) no.csv output.csv

make testAll给我

gcc topcgpas.c -o topcgpas
TEST: Incorrect number of arguments
./topcgpas 1 2 3
Usage ./topcgpas <sourcecsv> <outputcsv>
make: *** [makefile:16: test1] Error 1

但是,据我了解,它应该完成所有测试的执行。

【问题讨论】:

  • @EugeneSh。是的,它部分回答了我的问题,但我想从输出中删除 make: *** [makefile:16: test1] Error 1

标签: c bash makefile


【解决方案1】:

没有。默认情况下,Make 将在第一个失败的目标上退出。如果你想继续尝试并运行它可以运行的所有规则(它当然不会尝试构建任何依赖于失败的先决条件的目标),你可以添加 -k 选项。

https://www.gnu.org/software/make/manual/html_node/Errors.html

在我看来,您正在尝试进行负面测试。这意味着如果操作失败,您希望测试成功,如果操作成功,则失败。在这种情况下,您不想忽略错误,而是想反转它。这应该有效:

test1:
         @echo "TEST: Incorrect number of arguments"
         if ./$(TARGET) 1 2 3; then false; fi

【讨论】:

  • 我怎样才能省略像make: [makefile:32: test5] Error 1 (ignored)这样的行。
  • 无法删除该消息,您所能做的就是更改您的配方,以便 make 不会看到失败,除非您愿意。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-28
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多