__init__.py 3.1 KB
Newer Older
1
import logging
2
import os
3
import pkg_resources
4
import sys
C
Cleber Rosa 已提交
5
import unittest.mock
6 7


8 9 10 11 12 13
#: The base directory for the avocado source tree
BASEDIR = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))

#: The name of the avocado test runner entry point
AVOCADO = os.environ.get("UNITTEST_AVOCADO_CMD",
                         "%s ./scripts/avocado" % sys.executable)
14 15


16 17 18 19 20 21 22 23 24 25 26 27 28 29
def python_module_available(module_name):
    '''
    Checks if a given Python module is available

    :param module_name: the name of the module
    :type module_name: str
    :returns: if the Python module is available in the system
    :rtype: bool
    '''
    try:
        pkg_resources.require(module_name)
        return True
    except pkg_resources.DistributionNotFound:
        return False
30 31


32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
def setup_avocado_loggers():
    """
    Setup avocado loggers to contain at least one logger

    This is required for tests that directly utilize avocado.Test classes
    because they require those loggers to be configured. Without this
    it might (py2) result in infinite recursion while attempting to log
    "No handlers could be found for logger ..." message.
    """
    for name in ('', 'avocado.test', 'avocado.app'):
        logger = logging.getLogger(name)
        if not logger.handlers:
            logger.handlers.append(logging.NullHandler())


47 48 49 50 51 52 53 54 55
#: The plugin module names and directories under optional_plugins
PLUGINS = {'varianter_yaml_to_mux': 'avocado-framework-plugin-varianter-yaml-to-mux',
           'runner_remote': 'avocado-framework-plugin-runner-remote',
           'runner_vm': 'avocado-framework-plugin-runner-vm',
           'varianter_cit': 'avocado-framework-plugin-varianter-cit',
           'html': 'avocado-framework-plugin-result-html'}


def test_suite(base_selftests=True, plugin_selftests=None):
56 57 58 59 60
    '''
    Returns a test suite with all selftests found

    This includes tests on available optional plugins directories

61 62 63 64 65
    :param base_selftests: if the base selftests directory should be included
    :type base_selftests: bool
    :param plugin_selftests: the list optional plugin directories to include
                             or None to include all
    :type plugin_selftests: list or None
66 67 68 69 70 71
    :rtype: unittest.TestSuite
    '''
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    selftests_dir = os.path.dirname(os.path.abspath(__file__))
    basedir = os.path.dirname(selftests_dir)
72 73 74 75 76 77 78 79 80
    if base_selftests:
        for section in ('unit', 'functional', 'doc'):
            start_dir = os.path.join(selftests_dir, section)
            suite.addTests(loader.discover(start_dir=start_dir,
                                           top_level_dir=basedir))
    if plugin_selftests is None:
        plugin_selftests = PLUGINS.keys()
    for plugin_dir in plugin_selftests:
        plugin_name = PLUGINS.get(plugin_dir, None)
81 82 83 84 85
        if python_module_available(plugin_name):
            path = os.path.join(basedir, 'optional_plugins',
                                plugin_dir, 'tests')
            suite.addTests(loader.discover(start_dir=path, top_level_dir=path))
    return suite