Bash脚本自动测试程序输出 – C.

前端之家收集整理的这篇文章主要介绍了Bash脚本自动测试程序输出 – C.前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是编写脚本的新手,我无法弄清楚如何开始使用bash脚本,该脚本将根据预期输出自动测试程序的输出.

我想编写一个bash脚本,它将在一组测试输入上运行指定的可执行文件,例如in1 in2等,对应相应的预期输出,out1,out2等,并检查它们是否匹配.要测试的文件从stdin读取其输入并将其输出写入stdout.因此,在输入文件上执行测试程序将涉及I / O重定向.

将使用单个参数调用该脚本,该参数将是要测试的可执行文件名称.

我正在努力解决这个问题,所以任何帮助(链接到任何进一步解释我如何做到这一点的资源)将不胜感激.我显然已经尝试过自己寻找,但在这方面并不是很成功.

谢谢!

如果我得到你想要的东西;这可能会让你开始:

混合使用像diff这样的bash外部工具.

  1. #!/bin/bash
  2.  
  3. # If number of arguments less then 1; print usage and exit
  4. if [ $# -lt 1 ]; then
  5. printf "Usage: %s <application>\n" "$0" >&2
  6. exit 1
  7. fi
  8.  
  9. bin="$1" # The application (from command arg)
  10. diff="diff -iad" # Diff command,or what ever
  11.  
  12. # An array,do not have to declare it,but is supposedly faster
  13. declare -a file_base=("file1" "file2" "file3")
  14.  
  15. # Loop the array
  16. for file in "${file_base[@]}"; do
  17. # Padd file_base with suffixes
  18. file_in="$file.in" # The in file
  19. file_out_val="$file.out" # The out file to check against
  20. file_out_tst="$file.out.tst" # The outfile from test application
  21.  
  22. # Validate infile exists (do the same for out validate file)
  23. if [ ! -f "$file_in" ]; then
  24. printf "In file %s is missing\n" "$file_in"
  25. continue;
  26. fi
  27. if [ ! -f "$file_out_val" ]; then
  28. printf "Validation file %s is missing\n" "$file_out_val"
  29. continue;
  30. fi
  31.  
  32. printf "Testing against %s\n" "$file_in"
  33.  
  34. # Run application,redirect in file to app,and output to out file
  35. "./$bin" < "$file_in" > "$file_out_tst"
  36.  
  37. # Execute diff
  38. $diff "$file_out_tst" "$file_out_val"
  39.  
  40.  
  41. # Check exit code from prevIoUs command (ie diff)
  42. # We need to add this to a variable else we can't print it
  43. # as it will be changed by the if [
  44. # Iff not 0 then the files differ (at least with diff)
  45. e_code=$?
  46. if [ $e_code != 0 ]; then
  47. printf "TEST FAIL : %d\n" "$e_code"
  48. else
  49. printf "TEST OK!\n"
  50. fi
  51.  
  52. # Pause by prompt
  53. read -p "Enter a to abort,anything else to continue: " input_data
  54. # Iff input is "a" then abort
  55. [ "$input_data" == "a" ] && break
  56.  
  57. done
  58.  
  59. # Clean exit with status 0
  60. exit 0

编辑.

添加退出代码检查;还有一个很短的步行槽:

这将简短地做:

>检查是否给出了参数(bin / application)
>使用“基本名称”数组,循环并生成真正的文件名.

> I.e.:你得到数组(“file1”“file2”)

>在文件中:file1.in
>输出要验证的文件:file1.out
>输出文件:file1.out.tst
>在文件中:file2.in
> ……

>通过<执行应用程序并将文件重定向到stdin以供应用程序使用,并通过>将stdout从应用程序重定向到out文件测试.
>使用像diff这样的工具来测试它们是否相同.
>检查工具和打印消息中的退出/返回代码(FAIL / OK)
>提示继续.

任何和所有当然可以修改,删除等.

一些链接

> TLDP; Advanced Bash-Scripting Guide(this可以更具可读性)

> Arrays
> File test operators
> Loops and branches
> Exit-status
> ……

> bash-array-tutorial
> TLDP; Bash-Beginners-Guide

猜你在找的Bash相关文章