run 2.2 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'Lucas Meneghel Rodrigues <lmr@redhat.com>'

6 7 8
import logging
import os
import sys
9

10
from nose.selector import Selector
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
from nose.plugins import Plugin
from nose.plugins.attrib import AttributeSelector
import nose


logger = logging.getLogger(__name__)


class AvocadoTestSelector(Selector):

    def wantDirectory(self, dirname):
        return True

    def wantModule(self, module):
        return True

    def wantFile(self, filename):
28
        if not filename.endswith('.py'):
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
            return False

        skip_tests = []
        if self.config.options.skip_tests:
            skip_tests = self.config.options.skip_tests.split()

        if os.path.basename(filename)[:-3] in skip_tests:
            logger.debug('Skipping test: %s' % filename)
            return False

        if self.config.options.debug:
            logger.debug('Adding %s as a valid test' % filename)

        return True


class AvocadoTestRunner(Plugin):

    enabled = True
    name = 'avocado_test_runner'

    def configure(self, options, config):
        self.result_stream = sys.stdout

        config.logStream = self.result_stream
        self.testrunner = nose.core.TextTestRunner(stream=self.result_stream,
                                                   descriptions=True,
                                                   verbosity=2,
                                                   config=config)

    def options(self, parser, env):
        parser.add_option("--avocado-skip-tests",
                          dest="skip_tests",
                          default=[],
                          help='A space separated list of tests to skip')

    def prepareTestLoader(self, loader):
        loader.selector = AvocadoTestSelector(loader.config)

68
if __name__ == '__main__':
69 70 71 72 73 74 75 76 77
    if 'addplugins' in nose.main.__init__.func_code.co_varnames:
        nose.main(addplugins=[AvocadoTestRunner(),
                              AttributeSelector()])
    elif 'plugins' in nose.main.__init__.func_code.co_varnames:
        nose.main(plugins=[AvocadoTestRunner(),
                           AttributeSelector()])
    else:
        print("Unsupported nose API, can't proceed with testing...")
        sys.exit(1)