1#!/bin/bash
2
3# Usage info
4
5usage()
6{
7cat << EOF
8usage: $0 [options]
9
10This script runs the OpenCV tests on linux device.
11
12OPTIONS:
13   -h           Show this message
14   -c           Color output
15EOF
16}
17
18# Parse options
19
20COLOR_OUTPUT=0
21while getoptshcOPTION
22do
23    case $OPTION in
24        h)
25            usage
26            exit 0
27            ;;
28        c)
29            COLOR_OUTPUT=1
30            ;;
31        ?)
32            usage
33            exit 1
34            ;;
35    esac
36done
37
38# Text style
39
40if [ $COLOR_OUTPUT -eq 1 ]; then
41    TEXT_RED="$(tput setaf 1)"
42    TEXT_GREEN="$(tput setaf 2)"
43    TEXT_CYAN="$(tput setaf 6)"
44    TEXT_RESET="$(tput sgr0)"
45else
46    TEXT_RED=""
47    TEXT_GREEN=""
48    TEXT_CYAN=""
49    TEXT_RESET=""
50fi
51
52# Test binaries and data paths
53
54OPENCV_TEST_PATH=@CMAKE_INSTALL_PREFIX@/@OPENCV_TEST_INSTALL_PATH@
55OPENCV_PYTHON_TESTS=@OPENCV_PYTHON_TESTS_LIST@
56export OPENCV_TEST_DATA_PATH=@CMAKE_INSTALL_PREFIX@/share/OpenCV/testdata
57
58# Run tests
59
60SUMMARY_STATUS=0
61FAILED_TESTS=""
62PASSED_TESTS=""
63
64for t in "$OPENCV_TEST_PATH/"opencv_test_* "$OPENCV_TEST_PATH/"opencv_perf_*;
65do
66    test_name=`basename "$t"`
67    report="$test_name-`date --rfc-3339=date`.xml"
68
69    cmd="$t --perf_min_samples=1 --perf_force_samples=1 --gtest_output=xml:\"$report\""
70
71    seg_reg="s/^/${TEXT_CYAN}[$test_name]${TEXT_RESET} /"                     # append test name
72    if [ $COLOR_OUTPUT -eq 1 ]; then
73        seg_reg="${seg_reg};s/\[==========\]/${TEXT_GREEN}&${TEXT_RESET}/g"   # green for [==========]
74        seg_reg="${seg_reg};s/\[----------\]/${TEXT_GREEN}&${TEXT_RESET}/g"   # green for [----------]
75        seg_reg="${seg_reg};s/\[ RUN      \]/${TEXT_GREEN}&${TEXT_RESET}/g"   # green for [ RUN      ]
76        seg_reg="${seg_reg};s/\[       OK \]/${TEXT_GREEN}&${TEXT_RESET}/g"   # green for [       OK ]
77        seg_reg="${seg_reg};s/\[  FAILED  \]/${TEXT_RED}&${TEXT_RESET}/g"     # red   for [  FAILED  ]
78        seg_reg="${seg_reg};s/\[  PASSED  \]/${TEXT_GREEN}&${TEXT_RESET}/g"   # green for [  PASSED  ]
79    fi
80
81    echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} RUN : $cmd"
82    $cmd | sed -r "$seg_reg"
83    ret=${PIPESTATUS[0]}
84    echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} RETURN_CODE : $ret"
85
86    if [ $ret -ne 0 ]; then
87        echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} ${TEXT_RED}FAILED${TEXT_RESET}"
88        SUMMARY_STATUS=1
89        FAILED_TESTS="$FAILED_TESTS $test_name"
90    else
91        echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} ${TEXT_GREEN}OK${TEXT_RESET}"
92        PASSED_TESTS="$PASSED_TESTS $test_name"
93    fi
94
95    echo ""
96done
97
98for t in $OPENCV_PYTHON_TESTS;
99do
100    test_name=`basename "$t"`
101    report="$test_name-`date --rfc-3339=date`.xml"
102
103    cmd="py.test --junitxml $report \"$OPENCV_TEST_PATH\"/$t"
104
105    seg_reg="s/^/${TEXT_CYAN}[$test_name]${TEXT_RESET} /"                 # append test name
106
107    echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} RUN : $cmd"
108    eval "$cmd" | sed -r "$seg_reg"
109
110    ret=${PIPESTATUS[0]}
111    echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} RETURN_CODE : $ret"
112
113    if [ $ret -ne 0 ]; then
114        echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} ${TEXT_RED}FAILED${TEXT_RESET}"
115        SUMMARY_STATUS=1
116        FAILED_TESTS="$FAILED_TESTS $test_name"
117    else
118        echo "${TEXT_CYAN}[$test_name]${TEXT_RESET} ${TEXT_GREEN}OK${TEXT_RESET}"
119        PASSED_TESTS="$PASSED_TESTS $test_name"
120    fi
121
122    echo ""
123done
124
125# Remove temporary test files
126
127rm -f /tmp/__opencv_temp.*
128
129# Report final status
130
131echo "${TEXT_CYAN}===============================================================${TEXT_RESET}"
132echo "${TEXT_CYAN}PASSED TESTS : $PASSED_TESTS${TEXT_RESET}"
133echo "${TEXT_CYAN}FAILED TESTS : $FAILED_TESTS${TEXT_RESET}"
134if [ $SUMMARY_STATUS -eq 0 ]; then
135    echo "${TEXT_GREEN}STATUS : OK${TEXT_RESET}"
136    echo "${TEXT_GREEN}STATUS : All OpenCV tests finished successfully${TEXT_RESET}"
137else
138    echo "${TEXT_RED}STATUS : FAIL${TEXT_RESET}"
139    echo "${TEXT_RED}STATUS : OpenCV tests finished with status $SUMMARY_STATUS${TEXT_RESET}"
140fi
141
142exit $SUMMARY_STATUS
143