Modify for testing all unittest (#2288)
author남궁석/동작제어Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Fri, 17 Aug 2018 05:56:57 +0000 (14:56 +0900)
committer오형석/동작제어Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Fri, 17 Aug 2018 05:56:57 +0000 (14:56 +0900)
Fix `runall` argument type and defualt value
Fix typo `unittestall` to `unittestall_on`
Add running all unittests in `run_unittest.py`
Revise the structure of `run_unittest.py` and `run_unittest.sh` for running all unittests

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
tools/test_driver/py/run_unittest.py
tools/test_driver/py/test_driver.py
tools/test_driver/run_unittest.sh

index 44bcbaf..d60a6a2 100755 (executable)
@@ -17,6 +17,7 @@
 import os
 import sys
 import argparse
+import subprocess
 
 
 def get_parsed_options():
@@ -50,19 +51,24 @@ def get_parsed_options():
     parser.add_argument(
         "--runall",
         action="store_true",
-        type=str,
         dest="runall",
+        default=False,
         help="run all unittest and ignore skiplist")
 
     options = parser.parse_args()
     return options
 
 
-def get_gtest_option(report_dir, test_bin, unittest_dir):
+def get_gtest_option(report_dir, test_bin, unittest_dir=None):
     # Set path to save test result
     output_option = "--gtest_output=xml:{report_dir}/{test_bin}.xml".format(
         report_dir=report_dir, test_bin=test_bin)
 
+    # Set filter to run only one unit test, for runall unittest
+    if '.' in test_bin:
+        return output_option + " " + "--gtest_filter={test_list_item}".format(
+            test_list_item=test_bin)
+
     # Set filter not to run *.skip unit tests
     filter_option = ""
     skiplist_path = "{unittest_dir}/{test_bin}.skip".format(
@@ -77,6 +83,28 @@ def get_gtest_option(report_dir, test_bin, unittest_dir):
     return output_option + " " + filter_option
 
 
+def get_test_list_items(unittest_dir, test_bin):
+    cmd_output = subprocess.check_output(
+        "{unittestdir}/{testbin} --gtest_list_tests".format(
+            unittestdir=unittest_dir, testbin=test_bin),
+        shell=True)
+    all_test_list = str(cmd_output).replace('\\n', ' ').split()
+    all_test_list[0] = all_test_list[0][2:]
+
+    category = ""
+    item = ""
+    test_list_items = []
+    for verbose_line in all_test_list:
+        if verbose_line[-1] == '.':
+            category = verbose_line
+        else:
+            item = "{category}{verbose_line}".format(
+                category=category, verbose_line=verbose_line)
+            test_list_items.append(item)
+
+    return test_list_items
+
+
 # Just call this function when running unit test in test_driver.py
 def run_unittest(unittest_dir, report_dir, ldlibrary_path, runall):
     if unittest_dir == "" or unittest_dir == None:
@@ -96,15 +124,11 @@ def run_unittest(unittest_dir, report_dir, ldlibrary_path, runall):
     print("Unittest start")
     print("============================================")
 
-    # Run all unit tests in unittest_dir except *.skip test bin
+    # Run all unit tests in unittest_dir
     unittest_result = 0
     all_test_bin = (t for t in os.listdir(unittest_dir)
                     if len(t) < 5 or t[-5:] != ".skip")
 
-    if runall:
-        # TODO: run all unittest using --gtest_list_tests option
-        sys.exit(0)
-
     for idx, test_bin in enumerate(all_test_bin):
         num_unittest = idx + 1
         print("============================================")
@@ -112,11 +136,26 @@ def run_unittest(unittest_dir, report_dir, ldlibrary_path, runall):
             num_unittest=num_unittest, test_bin=test_bin))
         print("============================================")
 
-        cmd = "{unittest_dir}/{test_bin} {gtest_option}".format(
-            unittest_dir=unittest_dir,
-            test_bin=test_bin,
-            gtest_option=get_gtest_option(report_dir, test_bin, unittest_dir))
-        ret = os.system(cmd)
+        ret = 0
+
+        # Run all unit tests ignoring skip list
+        if runall:
+            test_list_items = get_test_list_items(unittest_dir, test_bin)
+            for test_item in test_list_items:
+                cmd = "{unittest_dir}/{test_bin} {gtest_option}".format(
+                    unittest_dir=unittest_dir,
+                    test_bin=test_bin,
+                    gtest_option=get_gtest_option(report_dir, test_item))
+                result = os.system(cmd)
+                if result != 0:
+                    ret = result
+        # Run all unit tests except skip list
+        else:
+            cmd = "{unittest_dir}/{test_bin} {gtest_option}".format(
+                unittest_dir=unittest_dir,
+                test_bin=test_bin,
+                gtest_option=get_gtest_option(report_dir, test_bin, unittest_dir))
+            ret = os.system(cmd)
 
         if ret != 0:
             unittest_result = ret
index 4a6fdb7..9ed97d2 100755 (executable)
@@ -183,13 +183,8 @@ def run_unittest(options):
         artifactpath=options.artifactpath,
         reportdir=options.reportdir,
         unittestdir=options.unittestdir)
-    if options.unittestall:
-        cmd = "{artifactpath}/tools/test_driver/run_unittest.sh \
-                --reportdir={reportdir} \
-                --unittestdir={unittestdir} \ --runall".format(
-            artifactpath=options.artifactpath,
-            reportdir=options.reportdir,
-            unittestdir=options.unittestdir)
+    if options.unittestall_on:
+        cmd += " --runall"
     os.system(cmd)
 
 
index a557f4f..3cdab67 100755 (executable)
@@ -66,36 +66,34 @@ echo "Unittest start"
 echo "============================================"
 
 num_unittest=0
+for TEST_BIN in `ls $UNITTEST_TEST_DIR --hide=*.skip`; do
+    num_unittest=$((num_unittest+1))
+    echo "============================================"
+    echo "Starting set $num_unittest: $TEST_BIN..."
+    echo "============================================"
+    TEMP_UNITTEST_RESULT=0
 
-if [ "$UNITTEST_RUN_ALL" == "true" ]; then
-
-    TEST_LIST_CATEGORY=""
-    TEST_LIST_ITEM=""
-
-    for TEST_BIN in `ls $UNITTEST_TEST_DIR --hide=*.skip`; do
+    if [ "$UNITTEST_RUN_ALL" == "true" ]; then
         for TEST_LIST_VERBOSE_LINE in $($UNITTEST_TEST_DIR/$TEST_BIN --gtest_list_tests); do
             if [[ $TEST_LIST_VERBOSE_LINE == *\. ]]; then
                 TEST_LIST_CATEGORY=$TEST_LIST_VERBOSE_LINE
             else
                 TEST_LIST_ITEM="$TEST_LIST_CATEGORY""$TEST_LIST_VERBOSE_LINE"
                 $UNITTEST_TEST_DIR/$TEST_BIN --gtest_filter=$TEST_LIST_ITEM --gtest_output="xml:$UNITTEST_REPORT_DIR/$TEST_LIST_ITEM.xml"
+                ONE_UNITTEST_RESULT=$?
+                if [[ $ONE_UNITTEST_RESULT -ne 0 ]]; then
+                    TEMP_UNITTEST_RESULT=$ONE_UNITTEST_RESULT
+                fi
             fi
         done
-    done
-
-    exit 0
-fi
-
-for TEST_BIN in `ls $UNITTEST_TEST_DIR --hide=*.skip`; do
-    num_unittest=$((num_unittest+1))
-    echo "============================================"
-    echo "Starting set $num_unittest: $TEST_BIN..."
-    echo "============================================"
-    $UNITTEST_TEST_DIR/$TEST_BIN $(get_gtest_option)
-    TEMP_UNITTEST_RESULT=$?
+    else
+        $UNITTEST_TEST_DIR/$TEST_BIN $(get_gtest_option)
+        TEMP_UNITTEST_RESULT=$?
+    fi
+    
     if [[ $TEMP_UNITTEST_RESULT -ne 0 ]]; then
         UNITTEST_RESULT=$TEMP_UNITTEST_RESULT
-        echo "$TEST_BIN failed... return code: $UNITTEST_RESULT"
+        echo "$TEST_BIN failed... return code: $TEMP_UNITTEST_RESULT"
     fi
     echo "============================================"
     echo "Finishing set $num_unittest: $TEST_BIN..."