4 ## @author MyungJoo Ham <myungjoo.ham@gmail.com>
6 ## @brief This executes test groups and reports aggregated test results.
7 ## @return 0 if all PASSED. Positive if some FAILED.
8 ## @todo Separate GStreamer related functions as plugins
10 ## This uses sed, date, cmp
12 ## If there is no arguments specified, this will search for all "runTest.sh" in
13 ## the subdirectory of this file and regard them as the test groups.
15 ## If a testgroup (runTest.sh) returns 0 while there are failed testcase,
16 ## it implies that the failed testcases may be ignored and it's good to go.
18 ## If --help or -h is given, this will show detailed description.
22 ## @section intro Introduction
23 ## - Introduction : Shell Script Automated Tester
24 ## @section Program Program Name
25 ## - Program Name : ssat
26 ## - Program Details : SSAT is a software testing framework for test cases written in BASH shell scripts.
27 ## It can search for test scripts recursively from a given path and summarize the test results.
28 ## If there is any "critical" fail, ssat will return non-zero values on its exit.
29 ## @section INOUTPUT Input/output data
30 ## - INPUT : Test Cases (If not supplied, the current path is the root of test cases)
31 ## - OUTPUT : Summary of test results to stdout. Exit code of 0 if success, non-zero if not success.
32 ## @section CREATEINFO Code information
33 ## - Initial date : 2018/06/22
37 BASEPATH=`dirname "$0"`
38 BASENAME=`basename "$0"`
46 COUNTNEGATIVEPOSTFIX=""
48 date=`date +"%b %d %Y"`
50 ## @fn createTemplate()
51 ## @brief Generate runTest template file
53 ## Note that the generated template has no license.
54 ## The SSAT user may put their own license for the generated files.
55 ## I hereby grant the right to relicense the generated files.
56 function createTemplate() {
57 if [[ -f "runTest.sh" ]]
59 printf "Cannot create runTest.sh here. The file already exists at $(pwd).\n\n"
63 echo -e "#!/usr/bin/env bash\n\
65 ## @file runTest.sh\n\
66 ## @author MyungJoo Ham <myungjoo.ham@gmail.com>\n\
68 ## @brief This is a template file for SSAT test cases. You may designate your own license.\n\
70 if [[ \"\$SSATAPILOADED\" != \"1\" ]]\n\
74 search=\"ssat-api.sh\"\n\
76 printf \"\${Blue}Independent Mode\${NC}\\n\"\n\
78 testInit \$1 # You may replace this with Test Group Name\n\
80 #testResult 1 T1 \"Dummy Test1\"\n\
81 #callTestSuccess gst-launch-1.0 \"-q videotestsrc ! videoconvert ! autovideosink\" T2 \"This may run indefinitely\"\n\
82 #callCompareTest golden.log executeResult.log T3 \"The two files must be same\" 0\n\
84 report\n" > runTest.sh
97 printf "usage: ${BASENAME} [--help] [<path>] [--testcase <filename>] [--nocolor] [--showstdout] [--createtemplate] [--countnegative <postfix>] \n\n"
98 printf "These are common ${Red}ssat${NC} commands used:\n\n"
99 printf "Test all test-groups in the current ($(pwd)) directory, recursively\n"
100 printf " (no options specified)\n"
101 printf " $ ${BASENAME}\n"
103 printf "Test all test-groups in the specified directory, recursively\n"
105 printf " $ ${BASENAME} /home/username/test\n"
106 printf " If there are multiple paths, the last one will be used\n"
108 printf "Search for \"filename\" as the testcase scripts\n"
109 printf " --testcase or -t\n"
110 printf " $ ${BASENAME} --testcase cases.sh\n"
111 printf " Search for cases.sh instead of runTest.sh\n"
113 printf "Do not emit colored text\n"
114 printf " --nocolor or -n\n"
116 printf "Show stdout of test cases\n"
117 printf " --showstdout or -s\n"
119 printf "Create a template 'runTest.sh' test group at your current directory\n"
120 printf " --createtemplate or -c\n"
122 printf "Show progress during execution\n"
123 printf " --progress or -p or -p=(0,1,9)\n"
124 printf " '0' : Do not print logs in progress. If -p is not given, -p=0 is assumed.\n"
125 printf " '1' : Print test group names only in progress.\n"
126 printf " '2-9' : Print all logs in progress. If -p is given without numbers, -p=9 is used.\n"
127 printf " $ ${BASENAME} -p=1\n"
128 printf " $ ${BASENAME} --progress=9 (equal to --progress) \n"
130 printf "Enable valgrind to perform memcheck\n"
131 printf " --enable-valgrind or -vg\n"
133 printf "Shows this message\n"
134 printf " --help or -h\n"
135 printf " $ ${BASENAME} --help \n"
137 printf "Count negative test cases with the given postfix\n"
138 printf " --countnegative or -cn\n"
139 printf " $ ${BASENAME} --countnegative _n\n"
140 printf " $ ${BASENAME} -cn _n\n"
142 printf "Write result summary as a file\n"
143 printf " --summary <filename>\n"
158 COUNTNEGATIVEPOSTFIX="$2"
159 if [[ "${COUNTNEGATIVEPOSTFIX}" == "" ]]
161 printf "${BASENAME} -cn or --countnegative requires postfix.\n\n"
175 -p|-p=*|--progress|--progress=*)
176 if [[ $key == "-p" || $key == "--progress" ]]
179 printf "Progress Log level is not given. Print all logs in progress.\n"
181 PROGRESSLOGLEVEL=${key#*=}
182 printf "Given progress log level is ${PROGRESSLOGLEVEL}.\n"
186 -vg|--enable-valgrind)
195 *) # Unknown, which is probably target (the path to root-dir of test groups).
201 source ${BASEPATH}/ssat-api.sh
203 if [[ "${#TARGET}" -eq "0" ]]
219 while read -d $'\0' file
221 CASEBASEPATH=`dirname "$file"`
222 CASENAME=`basename "$CASEBASEPATH"`
229 if [[ ${PROGRESSLOGLEVEL} -ge 1 ]]; then
230 printf "[Starting] $CASENAME\n"
232 pushd $CASEBASEPATH > /dev/null
233 output=$(. $file $CASEBASEPATH)
237 logfile="${output##*$'\n'}"
238 resultlog=$(<$logfile)
239 effectiveOutput=`printf "$resultlog" | sed '$d'`
240 log="$log$effectiveOutput\n"
242 lastline=`printf "${resultlog}" | sed '$!d'`
253 TNtcpass=$((TNtcpass+Npass))
254 TNtcfail=$((TNtcfail+Nfail))
255 TNtcignore=$((TNtcignore+Nignore))
256 TNtcneg=$((TNtcneg+Nneg))
258 TNgroup=$((TNgroup+1))
259 if [[ "$retcode" -eq "0" ]]
261 TNgrouppass=$((TNgrouppass+1))
262 groupLog="${groupLog}${LightGreen}[PASSED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
264 TNgroupfail=$((TNgroupfail+1))
265 groupLog="${groupLog}${Red}[FAILED]${NC} ${Blue}${CASENAME}${NC} ($Npass passed among $Ntc cases)\n"
270 done < <(find $TARGET -name $TESTCASE -print0)
272 printf "\n\n==================================================\n\n"
274 printf "==================================================\n\n"
276 printf "==================================================\n"
280 if (( ${COUNTNEGATIVE} == 1 ))
282 total=$((TNtcpass+TNtcfail+TNtcignore))
283 pos=$((total-TNtcneg))
284 ADDITIONALSTRING="${ADDITIONALSTRING} | Positive: ${pos} / Negative: ${TNtcneg}"
285 ADDITIONALSUMMARY="${ADDITIONALSUMMARY}, negative=${TNtcneg}"
288 if [ "${SUMMARYFILENAME}" != "" ]
290 echo "passed=${TNtcpass}, failed=${TNtcfail}, ignored=${TNtcignore}${ADDITIONALSUMMARY}" > "${SUMMARYFILENAME}"
292 if (( ${TNgroupfail} == 0 ))
294 printf "${LightGreen}[PASSED] ${Blue}All Test Groups (${TNgroup}) Passed!${NC}\n"
295 printf " TC Passed: ${TNtcpass} / Failed: ${TNtcfail} / Ignored: ${TNtcignore} ${ADDITIONALSTRING}\n\n";
298 printf "${Red}[FAILED] ${Purple}There are failed test groups! (${TNgroupfail})${NC}\n"
299 printf " TC Passed: ${TNtcpass} / Failed: ${TNtcfail} / Ignored: ${TNtcignore} ${ADDITIONALSTRING}\n\n";
302 # gather reports & publish them.