簡體   English   中英

如何遍歷Makefile中的n個測試用例?

[英]How To Loop through n Test cases In Makefile?

我希望我的Makefile為我自動化測試。 基本上,我會在代碼上運行一堆測試用例。 我希望用戶指定測試用例的數量,而不是進行硬編碼。

基本上我想要這樣的東西:

gcc main.c -o main

./main < test1.txt > output1.txt
./main < test2.txt > output2.txt
./main < test3.txt > output3.txt
./main < test4.txt > output4.txt
.
.
.
./main < test<n>.txt > output<n>.txt #for some value n

並將其變成這樣:

gcc main.c -o main

#of course this wouldn't be the syntax, but I just need the Makefile version of a loop, where all one has to do is change the n value 
for(int i = 0; i < n+1; i++){
   ./main < test<i>.txt > output<i>.txt;
}

謝謝 :)

現在已更新,可以正確回答問題

您可能想要做的(看起來)是讓makefile為您做各種事情:

# Target to build "main" its the first target and therefore the default 
# call "make" to run
main:
    @gcc main.c -o main

# Arbitrary max number of tests, can be overwritten by passing the variable in
NUM_TESTS=100
# Find all the tests, put them into an ordered list, then take the first 
# 1 to NUM_TESTS of them. Finally substitute test* for output*
TEST_OUTPUTS=$(subst test,output,$(wordlist 1,$(NUM_TESTS),$(sort $(wildcard test*.txt))))

# Target to do your testing, call "make test NUM_TESTS=3" or "make test" 
# to run all tests (up to 100).
.PHONY: test
test: $(TEST_OUTPUTS)

# Pattern rule to run each test - you don't call this directly
# Note: this has a dependency on main so if main is not built it 
# will get built first
output%.txt: test%.txt main
    @./main < $< > $@

# Target to clean up output files, call "make clean"
.PHONY: clean
clean:
    rm -f main
    rm -f $(TEST_OUTPUTS)

使用者:

  • make build -建立主要
  • make test -運行發現的所有測試,最多100次(可以更改的最大值)
  • make test NUM_TESTS=3運行前3個測試(如果找到了)
  • make test NUM_TESTS=3 -j6與以前相同,但運行6個並行作業(或使用-j進行盡可能多的並行作業)-即並行運行測試

說明:模式規則將根據文件test * .txt生成文件output * .txt。 但是我們要為此調用規則outputX.txt ,通過搜索所有輸出文件(在TEST_OUTPUTS變量中),然后選擇所需的測試數量,來生成輸出文件列表。 我們可以通過傳入一個變量來完成此操作,或者如果我們不傳入一個變量,那么它最多可以進行100次測試(或您設置的最大值)。

注意:我沒有運行它,所以我認為是偽代碼,但是應該很接近)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM