ci ttfw: Consistently handle unexpected exceptions in test cases

This commit is contained in:
Angus Gratton
2020-12-31 10:58:28 +11:00
parent cee0e17b7f
commit 441d1306be
4 changed files with 20 additions and 9 deletions

View File

@@ -439,7 +439,7 @@ class BaseDUT(object):
if isinstance(data, type(u'')): if isinstance(data, type(u'')):
try: try:
data = data.encode('utf-8') data = data.encode('utf-8')
except Exception as e: except UnicodeEncodeError as e:
print(u'Cannot encode {} of type {}'.format(data, type(data))) print(u'Cannot encode {} of type {}'.format(data, type(data)))
raise e raise e
return data return data

View File

@@ -15,7 +15,6 @@
""" Interface for test cases. """ """ Interface for test cases. """
import os import os
import time import time
import traceback
import functools import functools
import socket import socket
from datetime import datetime from datetime import datetime
@@ -195,10 +194,7 @@ def test_method(**kwargs):
# if finish without exception, test result is True # if finish without exception, test result is True
result = True result = True
except Exception as e: except Exception as e:
# handle all the exceptions here Utility.handle_unexpected_exception(junit_test_case, e)
traceback.print_exc()
# log failure
junit_test_case.add_failure_info(str(e) + ":\r\n" + traceback.format_exc())
finally: finally:
# do close all DUTs, if result is False then print DUT debug info # do close all DUTs, if result is False then print DUT debug info
close_errors = env_inst.close(dut_debug=(not result)) close_errors = env_inst.close(dut_debug=(not result))

View File

@@ -2,6 +2,7 @@ from __future__ import print_function
import os.path import os.path
import sys import sys
import time import time
import traceback
from .. import Env from .. import Env
@@ -95,3 +96,16 @@ def load_source(path):
sys.path.remove(dir) sys.path.remove(dir)
__LOADED_MODULES[path] = ret __LOADED_MODULES[path] = ret
return ret return ret
def handle_unexpected_exception(junit_test_case, exception):
"""
Helper to log & add junit result details for an unexpected exception encountered
when running a test case.
Should always be called from inside an except: block
"""
traceback.print_exc()
# AssertionError caused by an 'assert' statement has an empty string as its 'str' form
e_str = str(exception) if str(exception) else repr(exception)
junit_test_case.add_failure_info("Unexpected exception: {}\n{}".format(e_str, traceback.format_exc()))

View File

@@ -24,6 +24,7 @@ import argparse
import threading import threading
from tiny_test_fw import TinyFW, Utility, Env, DUT from tiny_test_fw import TinyFW, Utility, Env, DUT
from tiny_test_fw.Utility import handle_unexpected_exception
import ttfw_idf import ttfw_idf
UT_APP_BOOT_UP_DONE = "Press ENTER to see the list of tests." UT_APP_BOOT_UP_DONE = "Press ENTER to see the list of tests."
@@ -317,7 +318,7 @@ def run_unit_test_cases(env, extra_data):
except TestCaseFailed: except TestCaseFailed:
failed_cases.append(format_case_name(one_case)) failed_cases.append(format_case_name(one_case))
except Exception as e: except Exception as e:
junit_test_case.add_failure_info("Unexpected exception: " + str(e)) handle_unexpected_exception(junit_test_case, e)
failed_cases.append(format_case_name(one_case)) failed_cases.append(format_case_name(one_case))
finally: finally:
TinyFW.JunitReport.update_performance(performance_items) TinyFW.JunitReport.update_performance(performance_items)
@@ -517,7 +518,7 @@ def run_multiple_devices_cases(env, extra_data):
result = run_one_multiple_devices_case(duts, ut_config, env, one_case, result = run_one_multiple_devices_case(duts, ut_config, env, one_case,
one_case.get('app_bin'), junit_test_case) one_case.get('app_bin'), junit_test_case)
except Exception as e: except Exception as e:
junit_test_case.add_failure_info("Unexpected exception: " + str(e)) handle_unexpected_exception(junit_test_case, e)
finally: finally:
if result: if result:
Utility.console_log("Success: " + format_case_name(one_case), color="green") Utility.console_log("Success: " + format_case_name(one_case), color="green")
@@ -677,7 +678,7 @@ def run_multiple_stage_cases(env, extra_data):
except TestCaseFailed: except TestCaseFailed:
failed_cases.append(format_case_name(one_case)) failed_cases.append(format_case_name(one_case))
except Exception as e: except Exception as e:
junit_test_case.add_failure_info("Unexpected exception: " + str(e)) handle_unexpected_exception(junit_test_case, e)
failed_cases.append(format_case_name(one_case)) failed_cases.append(format_case_name(one_case))
finally: finally:
TinyFW.JunitReport.update_performance(performance_items) TinyFW.JunitReport.update_performance(performance_items)