示例#1
0
class NoseSelectPlugin(Plugin):
    """Selects test to run based on tests names matching a pattern."""

    def options(self, parser, env):
        """Register command line options"""
        parser.add_option("-t", "--select-tests",
                          dest="selection_criteria", action="append",
                          default=list(),
                          metavar="SELECT",
                          help="Only run tests with a name matching a case-insensitive glob pattern (See fnmatch)")

    def _as_pattern(self, criterion):
        # transforms selection criteria in glob patterns
        return '*%s*' % criterion.lower().strip('*')

    def add_criterion(self, criterion):
        #used mostly for testing
        if not hasattr(self, 'selection_criteria'):
            self.selection_criteria = []
        self.selection_criteria.append(self._as_pattern(criterion))

    def configure(self, options, config):
        self.selection_criteria = [self._as_pattern(criterion) 
                                   for criterion in options.selection_criteria
                                   if criterion and criterion.strip()]

        if self.selection_criteria:
            self.enabled = True

        # use a base selector to ensure we are additive to the basic selection
        self.base_selector = Selector(config)
        self.base_selector.configure(config)
        # we use a mock for plugins to avoid our plugin to be called 
        # in a loop from the Selector (and avoid an infinite loop)
        self.base_selector.plugins = MockPlugins()

    def _is_selected(self, test_obj):
        """Return True if a test object should be selected based on criteria pattern."""
        if not test_obj:
            return
        if isinstance(test_obj, basestring):
            name = test_obj
        else:
            name = objname(test_obj)
        #log.debug('object name: %r' % name)
        if name:
            name = name.lower()
            matched = lambda pat: fnmatch(name, pat)
            selected = any(matched(pat) for pat in self.selection_criteria)
            #log.debug('selected:%r name: %r' % (selected, name,))
            return selected
        else:
            return False

    def wantMethod(self, method):
        return self.base_selector.wantMethod(method) and self._is_selected(method)

    def wantFunction(self, function):
        return self.base_selector.wantFunction(function) and self._is_selected(function)
示例#2
0
 def test_want_method(self):
     class Baz:
         def test_me(self):
             pass
         def test_too(self):
             pass
         def other(self):
             pass
         def test_not_test(self):
             pass
         test_not_test.__test__ = False
         
     s = Selector(Config())
     
     assert s.wantMethod(Baz.test_me)
     assert s.wantMethod(Baz.test_too)
     assert not s.wantMethod(Baz.other)
     assert not s.wantMethod(Baz.test_not_test), \
            "Failed to respect __test__ = False"
示例#3
0
 def test_want_method(self):
     class Baz:
         def test_me(self):
             pass
         def test_too(self):
             pass
         def other(self):
             pass
         def test_not_test(self):
             pass
         test_not_test.__test__ = False
         
     s = Selector(Config())
     
     assert s.wantMethod(Baz.test_me)
     assert s.wantMethod(Baz.test_too)
     assert not s.wantMethod(Baz.other)
     assert not s.wantMethod(Baz.test_not_test), \
            "Failed to respect __test__ = False"
示例#4
0
class NoseSelectPlugin(Plugin):
    """Selects test to run based on tests names matching a pattern."""
    def options(self, parser, env):
        """Register command line options"""
        parser.add_option(
            "-t",
            "--select-tests",
            dest="selection_criteria",
            action="append",
            default=list(),
            metavar="SELECT",
            help=
            "Only run tests with a name matching a case-insensitive glob pattern (See fnmatch)"
        )

    def _as_pattern(self, criterion):
        # transforms selection criteria in glob patterns
        return '*%s*' % criterion.lower().strip('*')

    def add_criterion(self, criterion):
        #used mostly for testing
        if not hasattr(self, 'selection_criteria'):
            self.selection_criteria = []
        self.selection_criteria.append(self._as_pattern(criterion))

    def configure(self, options, config):
        self.selection_criteria = [
            self._as_pattern(criterion)
            for criterion in options.selection_criteria
            if criterion and criterion.strip()
        ]

        if self.selection_criteria:
            self.enabled = True

        # use a base selector to ensure we are additive to the basic selection
        self.base_selector = Selector(config)
        self.base_selector.configure(config)
        # we use a mock for plugins to avoid our plugin to be called
        # in a loop from the Selector (and avoid an infinite loop)
        self.base_selector.plugins = MockPlugins()

    def _is_selected(self, test_obj):
        """Return True if a test object should be selected based on criteria pattern."""
        if not test_obj:
            return
        if isinstance(test_obj, six.string_types):
            name = test_obj
        else:
            name = objname(test_obj)
        #log.debug('object name: %r' % name)
        if name:
            name = name.lower()
            selected = any(
                fnmatch(name, pat) for pat in self.selection_criteria)
            #log.debug('selected:%r name: %r' % (selected, name,))
            return selected
        else:
            return False

    def wantMethod(self, method):
        return self.base_selector.wantMethod(method) and self._is_selected(
            method)

    def wantFunction(self, function):
        return self.base_selector.wantFunction(function) and self._is_selected(
            function)