From 957f1fe17eaefb53ada7d9fc42235d888094cff5 Mon Sep 17 00:00:00 2001
From: Lionel Gauthier <lionel.gauthier@eurecom.fr>
Date: Thu, 30 Apr 2015 08:57:23 +0000
Subject: [PATCH] Sebastian Held patch 15/0001-jUnit-like-test-results.patch

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@7303 818b1a75-f10b-46b9-bf7c-635c3b92a50f
---
 .../autotests/run_compilation_autotests.bash  | 24 ++++---
 cmake_targets/tools/test_helper               | 64 +++++++++++++++++++
 2 files changed, 80 insertions(+), 8 deletions(-)
 create mode 100644 cmake_targets/tools/test_helper

diff --git a/cmake_targets/autotests/run_compilation_autotests.bash b/cmake_targets/autotests/run_compilation_autotests.bash
index c54ba1e778..9963edead6 100755
--- a/cmake_targets/autotests/run_compilation_autotests.bash
+++ b/cmake_targets/autotests/run_compilation_autotests.bash
@@ -7,20 +7,26 @@ else
    exit 1
 fi
 
+# include the jUnit-like logging functions
+source $OPENAIR_DIR/cmake_targets/tools/test_helper
+
 test_compile() {
+    xUnit_start
     mkdir -p $tdir/$1/build
     cd $tdir/$1/build
     {
-	cmake ..
-	rm -f $3
-	make -j4 $2
+        cmake ..
+        rm -f $3
+        make -j4 $2
     } > $tdir/log/$1.txt 2>&1
     if [ -s $3 ] ; then
-     cp $3 $tdir/bin/`basename $3`.$1
-     echo_success "$1 test compiled"
-  else
-     echo_error "$1 test compilation failed"
-  fi
+        cp $3 $tdir/bin/`basename $3`.$1
+        echo_success "$1 test compiled"
+        xUnit_success "compilation" $1
+    else
+        echo_error "$1 test compilation failed"
+        xUnit_fail "compilation" $1
+    fi
 }
 
 tdir=$OPENAIR_DIR/cmake_targets/autotests
@@ -77,3 +83,5 @@ test_compile \
     test.0120 nasmesh \
     CMakeFiles/nasmesh/nasmesh.ko $tdir/bin/nasmesh.ko
 
+# write the test results into a file
+xUnit_write "$tdir/log/compilation_autotests.xml"
diff --git a/cmake_targets/tools/test_helper b/cmake_targets/tools/test_helper
new file mode 100644
index 0000000000..e408102364
--- /dev/null
+++ b/cmake_targets/tools/test_helper
@@ -0,0 +1,64 @@
+# source this file in a bash
+
+XUNIT_TESTSUITE_START=0
+XUNIT_START=0
+XUNIT_TOTAL=0
+XUNIT_FAILED=0
+XUNIT_SUCCESS=0
+XUNIT_TESTCASES_XML=""
+
+## Call this at the start of a testcase. 
+# \sa xUnit_fail()
+# \sa xUnit_success()
+xUnit_start() {
+  XUNIT_START=$(date +%s.%N)
+  if [ $XUNIT_TESTSUITE_START == 0 ]; then
+    # very first call: start of a testsuite
+    XUNIT_TESTSUITE_START=$XUNIT_START
+  fi
+}
+
+## Call this after the testcase finished with a failure.
+# \sa xUnit_success()
+# \pre xUnit_start() must have been called before
+# \param $1 classname
+# \param $2 testcase name
+xUnit_fail() {
+  currtime=$(date +%s.%N)
+  time=$(echo "$currtime - $XUNIT_START" | bc -l)
+  xml="<testcase classname='$1' name='$2' time='$time'><failure message='failed'/></testcase>"
+  XUNIT_TESTCASES_XML="$XUNIT_TESTCASES_XML $xml"
+  XUNIT_FAILED=$((XUNIT_FAILED+1))
+}
+
+## Call this after the testcase finished successfully.
+# \sa xUnit_fail()
+# \pre xUnit_start() must have been called before
+# \param $1 classname
+# \param $2 testcase name
+xUnit_success() {
+  currtime=$(date +%s.%N)
+  time=$(echo "$currtime - $XUNIT_START" | bc -l)
+  xml="<testcase classname='$1' name='$2' time='$time' />"
+  XUNIT_TESTCASES_XML="$XUNIT_TESTCASES_XML $xml"
+  XUNIT_SUCCESS=$((XUNIT_SUCCESS+1))
+}
+
+## Call this after all testcases have been completed.
+# This functions writes out the test report.
+# \param $1 filename
+xUnit_write() {
+  tests=$((XUNIT_FAILED+XUNIT_SUCCESS))
+  timestamp=$(date --iso-8601=seconds)
+  time=$(echo "$currtime - $XUNIT_TESTSUITE_START" | bc -l)
+  xml_header="<testsuites><testsuite errors='0' failures='$XUNIT_FAILED' hostname='$(hostname)' name='OAI' skipped='0' tests='$tests' time='$time' timestamp='$timestamp'>"
+  echo $xml_header > $1
+  echo $XUNIT_TESTCASES_XML >> $1
+  echo "</testsuite></testsuites>" >> $1
+  XUNIT_TESTSUITE_START=0
+  XUNIT_START=0
+  XUNIT_TOTAL=0
+  XUNIT_FAILED=0
+  XUNIT_SUCCESS=0
+  XUNIT_TESTCASES_XML=""
+}
\ No newline at end of file
-- 
GitLab