0

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

我将这篇文章用作我预期目的的参考指南。

这是一个想法的结构。

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

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

4

1 回答 1

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
于 2021-04-09T16:42:01.453 回答