Created new script for running all unit tests
[situare] / scripts / run_unit_tests.sh
1 #!/bin/bash
2
3 clear
4
5 # set default values
6 UNIT_TESTS_ROOT_DIR="../tests"
7 REPORT_PATH="../doc"
8 REPORT_FILE="unit_test_results.txt"
9 TEMP_FILE_EXEC_NOT_FOUND="temp_exec_not_found.txt"
10 TEMP_REPORT_FILE="temp_report.txt"
11
12 # check that directories are found
13 if [[ -d $REPORT_PATH && -d $UNIT_TESTS_ROOT_DIR ]]; then
14     # convert relational paths to absolute ones
15     UNIT_TESTS_ROOT_DIR_ABSOLUTE=`cd $UNIT_TESTS_ROOT_DIR; pwd`
16     REPORT_PATH_ABSOLUTE=`cd $REPORT_PATH; pwd`
17     REPORT="$REPORT_PATH_ABSOLUTE/$REPORT_FILE"
18     echo ""
19     echo "Running recursively all tests under $UNIT_TESTS_ROOT_DIR_ABSOLUTE"
20     echo "Saving results to $REPORT"
21     echo ""
22
23     # overwrite report summary file and write header
24     echo "##########################################" > $REPORT
25     echo "# Summary of unit tests executed" >> $REPORT
26     echo "# Date: `date`" >> $REPORT
27     echo "# User: `whoami`" >> $REPORT
28     echo "##########################################" >> $REPORT
29     echo "" >> $REPORT
30
31     # find all test .pro files pahts, cut .pro extension
32     UNIT_TEST_PROJECTS=(`find $UNIT_TESTS_ROOT_DIR_ABSOLUTE | egrep .pro$ | sed -e s/.pro//g`)
33
34     # run tests
35     for project in "${UNIT_TEST_PROJECTS[@]}"
36     do 
37         if [ -f $project ]; then
38             echo ""
39             # run unit test, save to temp file, ignore strerr stream
40             $project -silent -o $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE 2> /dev/null
41             # print to screen and append to summary
42             cat $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE
43             cat $REPORT_PATH_ABSOLUTE/$TEMP_REPORT_FILE >> $REPORT_PATH_ABSOLUTE/$REPORT_FILE
44             echo "" >> $REPORT_PATH_ABSOLUTE/$REPORT_FILE
45         else
46             # save path of missing test to temporary file
47             missing=(`echo $project | sed -e s,$UNIT_TESTS_ROOT_DIR_ABSOLUTE,,g`)
48             echo "$missing" >> "$REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND"
49         fi
50     done
51
52     if [ -f "$REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND" ]; then
53         # print list of missing test executables
54         echo ""
55         echo "###################################################"
56         echo "Executables for following unit tests were not found"
57         echo -e "###################################################\E[31m\E[1m" ## set red color & bold
58         cat $REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND
59         tput sgr0 # restore terminal defaults
60
61         # and save same list also to test summary file
62         echo "" >> $REPORT
63         echo "###################################################" >> $REPORT
64         echo "Executables for following unit tests were not found" >> $REPORT
65         echo "###################################################"  >> $REPORT
66         cat $REPORT_PATH_ABSOLUTE/$TEMP_FILE_EXEC_NOT_FOUND  >> $REPORT
67     fi
68
69     # remove temporary files
70     rm $REPORT_PATH_ABSOLUTE/temp_*.txt
71
72     # tell that script reached the end
73     echo "" >> $REPORT
74     echo "All done!" >> $REPORT
75     echo ""
76     echo "All done!"
77 else
78     echo "Paths for report and/or tests not found!"
79     echo "Script must be run from scripts directory"
80 fi
81 echo ""