Sha*_*abu 6 bash automated-tests
我是编写脚本的新手,我无法弄清楚如何开始使用bash脚本,该脚本将根据预期输出自动测试程序的输出.
我想编写一个bash脚本,它将在一组测试输入上运行指定的可执行文件,例如in1 in2等,对应相应的预期输出,out1,out2等,并检查它们是否匹配.要测试的文件从stdin读取其输入并将其输出写入stdout.因此,在输入文件上执行测试程序将涉及I/O重定向.
将使用单个参数调用该脚本,该参数将是要测试的可执行文件的名称.
我正在努力解决这个问题,所以任何帮助(链接到任何进一步解释我如何做到这一点的资源)将不胜感激.我显然已经尝试过自己寻找,但在这方面并不是很成功.
谢谢!
如果我得到你想要的东西; 这可能会让你开始:
混合使用bash +外部工具,如diff.
#!/bin/bash
# If number of arguments less then 1; print usage and exit
if [ $# -lt 1 ]; then
printf "Usage: %s <application>\n" "$0" >&2
exit 1
fi
bin="$1" # The application (from command arg)
diff="diff -iad" # Diff command, or what ever
# An array, do not have to declare it, but is supposedly faster
declare -a file_base=("file1" "file2" "file3")
# Loop the array
for file in "${file_base[@]}"; do
# Padd file_base with suffixes
file_in="$file.in" # The in file
file_out_val="$file.out" # The out file to check against
file_out_tst="$file.out.tst" # The outfile from test application
# Validate infile exists (do the same for out validate file)
if [ ! -f "$file_in" ]; then
printf "In file %s is missing\n" "$file_in"
continue;
fi
if [ ! -f "$file_out_val" ]; then
printf "Validation file %s is missing\n" "$file_out_val"
continue;
fi
printf "Testing against %s\n" "$file_in"
# Run application, redirect in file to app, and output to out file
"./$bin" < "$file_in" > "$file_out_tst"
# Execute diff
$diff "$file_out_tst" "$file_out_val"
# Check exit code from previous command (ie diff)
# We need to add this to a variable else we can't print it
# as it will be changed by the if [
# Iff not 0 then the files differ (at least with diff)
e_code=$?
if [ $e_code != 0 ]; then
printf "TEST FAIL : %d\n" "$e_code"
else
printf "TEST OK!\n"
fi
# Pause by prompt
read -p "Enter a to abort, anything else to continue: " input_data
# Iff input is "a" then abort
[ "$input_data" == "a" ] && break
done
# Clean exit with status 0
exit 0
Run Code Online (Sandbox Code Playgroud)
编辑.
添加退出代码检查; 还有一个很短的步行槽:
这将简短地做:
("file1" "file2")
你得到阵列file1.in
file1.out
file1.out.tst
file2.in
stdin
应用程序<
,并stdout
从应用程序重定向到输出文件测试>
.diff
来测试它们是否相同.任何和所有当然可以修改,删除等.
一些链接: