Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 04cbeef0 authored by Shiu Ng's avatar Shiu Ng Committed by Gerrit Code Review
Browse files

Merge "Keep track of the test context to be able to write test output to...

Merge "Keep track of the test context to be able to write test output to test-specific directories."
parents 57d768eb 4705580c
Loading
Loading
Loading
Loading
+60 −0
Original line number Diff line number Diff line
@@ -42,6 +42,28 @@ def get_current_context(depth=None):
    return _contexts[min(depth, len(_contexts) - 1)]


def append_test_context(test_class_name, test_name):
    """Add test-specific context to the _contexts stack.
    A test should should call append_test_context() at test start and
    pop_test_context() upon test end.

    Args:
        test_class_name: name of the test class.
        test_name: name of the test.
    """
    if _contexts:
        _contexts.append(TestCaseContext(test_class_name, test_name))


def pop_test_context():
    """Remove the latest test-specific context from the _contexts stack.
    A test should should call append_test_context() at test start and
    pop_test_context() upon test end.
    """
    if _contexts:
        _contexts.pop()


class TestContext(object):
    """An object representing the current context in which a test is executing.

@@ -160,5 +182,43 @@ class RootContext(TestContext):
        return ''


class TestCaseContext(TestContext):
    """A TestContext that represents a test case.

    Attributes:
        test_case: the name of the test case.
        test_class: the name of the test class.
    """

    def __init__(self, test_class, test_case):
        """Initializes a TestCaseContext for the given test case.

        Args:
            test_class: test-class name.
            test_case: test name.
        """
        self.test_class = test_class
        self.test_case = test_case

    @property
    def test_case_name(self):
        return self.test_case

    @property
    def test_class_name(self):
        return self.test_class

    @property
    def identifier(self):
        return '%s.%s' % (self.test_class_name, self.test_case_name)

    def _get_default_context_dir(self):
        """Gets the default output directory for this context.

        For TestCaseContexts, this will be the name of the test itself.
        """
        return self.test_case_name


# stack for keeping track of the current test context
_contexts = [RootContext()]
+3 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ from cert.gd_device_lib import generate_coverage_report_for_host

from facade import rootservice_pb2 as facade_rootservice

from blueberry.tests.gd.cert.context import get_current_context
from blueberry.tests.gd.cert.context import append_test_context, get_current_context, pop_test_context, ContextLevel
from blueberry.tests.gd.cert.gd_device import MOBLY_CONTROLLER_CONFIG_NAME as CONTROLLER_CONFIG_NAME
from blueberry.tests.gd.cert.tracelogger import TraceLogger

@@ -58,6 +58,7 @@ class GdBaseTestClass(base_test.BaseTestClass):
            self.cert_coverage_info = None

    def setup_test(self):
        append_test_context(test_class_name=self.TAG, test_name=self.current_test_info.name)
        self.log_path_base = get_current_context().get_full_output_path()
        self.verbose_mode = bool(self.user_params.get('verbose_mode', False))
        for config in self.controller_configs[CONTROLLER_CONFIG_NAME]:
@@ -140,6 +141,7 @@ class GdBaseTestClass(base_test.BaseTestClass):
            rootcanal_process=self.rootcanal_process,
            rootcanal_logger=self.rootcanal_logger,
            subprocess_wait_timeout_seconds=self.SUBPROCESS_WAIT_TIMEOUT_SECONDS)
        pop_test_context()

    @staticmethod
    def get_module_reference_name(a_module):