自动化测试脚本实践:基于 Bash 的模块化测试框架

#!/bin/bash # 获取当前脚本所在的目录 SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) # 检查 config.conf 是否存在 if [[ ! -f "${SCRIPT_DIR}/config.conf" ]]; then echo "config.conf not found!" exit 1 fi # 加载配置文件 source ${SCRIPT_DIR}/config.conf EXAMPLE1_RESULT=0 EXAMPLE2_RESULT=0 EXAMPLE3_RESULT=0 # 依赖软件安装 install() { if ! command -v figlet > /dev/null 2>&1; then echo "Writing the installation method." fi } test_example1() { read -p "example1 test results(y: pass - n: fail): " example1 if [[ "${example1}" == "N" ]] || [[ "${example1}" == "n" ]]; then EXAMPLE1_RESULT=1 echo -e "####################\033[31m example1: fail \033[0m#####################" echo "##############################################################" else EXAMPLE1_RESULT=0 echo -e "####################\033[32m example1: pass \033[0m#####################" echo "##############################################################" fi } test_example2() { read -p "example2 test results(y: pass - n: fail): " example2 if [[ "${example2}" == "N" ]] || [[ "${example2}" == "n" ]]; then EXAMPLE2_RESULT=1 echo -e "####################\033[31m example2: fail \033[0m#####################" echo "##############################################################" else EXAMPLE2_RESULT=0 echo -e "####################\033[32m example2: pass \033[0m#####################" echo "##############################################################" fi } test_example3() { sudo chmod 777 ${SCRIPT_DIR}/scripts/eth.sh sudo ${SCRIPT_DIR}/scripts/eth.sh eth0 if [[ $? -eq 0 ]]; then EXAMPLE3_RESULT=0 echo -e "####################\033[32m example3: pass \033[0m#####################" echo "##############################################################" else EXAMPLE3_RESULT=1 echo -e "####################\033[31m example3: fail \033[0m#####################" echo "##############################################################" fi } test_exit() { exit 11 } module_choice() { echo " " echo "**************** Test Module Selection (Failures Detected) ****************" echo " 0 (exit test)" if [[ ${example1_activation} == "0" ]]; then if [[ ${EXAMPLE1_RESULT} -eq 0 ]]; then echo -e " 1 (example1 test) [\033[32m pass \033[0m]" else echo -e " 1 (example1 test) [\033[31m fail \033[0m]" fi fi if [[ ${example2_activation} == "0" ]]; then if [[ ${EXAMPLE2_RESULT} -eq 0 ]]; then echo -e " 2 (example2 test) [\033[32m pass \033[0m]" else echo -e " 2 (example2 test) [\033[31m fail \033[0m]" fi fi if [[ ${example3_activation} == "0" ]]; then if [[ ${EXAMPLE3_RESULT} -eq 0 ]]; then echo -e " 3 (example3 test) [\033[32m pass \033[0m]" else echo -e " 3 (example3 test) [\033[31m fail \033[0m]" fi fi echo " R (Failure item test)" echo "***************************************************************************" read -p "please select a test module to rerun or exit: " MODULE_CHOICE } module_test() { MODULE_CHOICE=$(echo "$MODULE_CHOICE" | tr '[:upper:]' '[:lower:]') case ${MODULE_CHOICE} in 0) test_exit ;; 1) if [[ ${example1_activation} == "0" ]]; then test_example1 fi ;; 2) if [[ ${example2_activation} == "0" ]]; then test_example2 fi ;; 3) if [[ ${example3_activation} == "0" ]]; then test_example3 fi ;; r) # Failure item选项,重新测试所有失败的项 if [[ ${EXAMPLE1_RESULT} -eq 1 ]]; then test_example1 fi if [[ ${EXAMPLE2_RESULT} -eq 1 ]]; then test_example2 fi if [[ ${EXAMPLE3_RESULT} -eq 1 ]]; then test_example3 fi ;; # *) # echo "Invalid choice. Please try again." # ;; esac } check_all_pass() { local test_results=( ${EXAMPLE1_RESULT} ${EXAMPLE2_RESULT} ${EXAMPLE3_RESULT} ) all_zero=true # 如果有一个结果等于1,标记为false for all_result in "${test_results[@]}"; do if [[ "${all_result}" -eq 1 ]]; then all_zero=false break fi done # 获取全部测试结果 0通过 1不通过 if [[ "$all_zero" == true ]]; then return 0 # 返回0表示所有结果为0 else return 1 # 返回1表示至少有一个结果不为0 fi } test_single() { while true; do module_choice module_test check_all_pass if [[ $? -eq 0 ]]; then echo "****************************************************************" echo -e "\033[32m$(figlet "PASS")\033[0m" echo "****************************************************************" break fi sleep 1 done } test_all() { echo " " echo "******************** Test Execution Started ********************" echo "****************************************************************" echo " " if [[ ${example1_activation} == "0" ]]; then test_example1 sleep 1 fi if [[ ${example2_activation} == "0" ]]; then test_example2 sleep 1 fi if [[ ${example3_activation} == "0" ]]; then test_example3 sleep 1 fi echo " " echo " " echo "********************* Test Results Summary *********************" echo "****************************************************************" if [[ ${example1_activation} == "0" ]]; then if [[ ${EXAMPLE1_RESULT} -eq 0 ]]; then echo -e "example1: \033[32m pass \033[0m" else echo -e "example1: \033[31m fail \033[0m" fi fi if [[ ${example2_activation} == "0" ]]; then if [[ ${EXAMPLE2_RESULT} -eq 0 ]]; then echo -e "example2: \033[32m pass \033[0m" else echo -e "example2: \033[31m fail \033[0m" fi fi if [[ ${example3_activation} == "0" ]]; then if [[ ${EXAMPLE3_RESULT} -eq 0 ]]; then echo -e "example3: \033[32m pass \033[0m" else echo -e "example3: \033[31m fail \033[0m" fi fi echo " " check_all_pass if [[ $? -eq 0 ]]; then echo "****************************************************************" echo -e "\033[32m$(figlet "PASS")\033[0m" echo "****************************************************************" else echo "****************************************************************" echo -e "\033[31m$(figlet "FAIL")\033[0m" echo "****************************************************************" echo " " echo " " fi } send_result() { echo "Upload test results." } main() { install test_all check_all_pass if [[ $? -eq 1 ]]; then test_single fi send_result } main
上一篇:MFC小游戏九:错误处理界面


下一篇:C#使用Process打开外部程序或外部文件