nose2 / nose2 / plugins /

import os
import pickle
import re
from import Plugin

class TestId(Plugin):
    configSection = 'testid'
    commandLineSwitch = ('I', 'with-id', 'Add test ids to output')
    idpat = re.compile(r'(\d+)')

    def __init__(self):
        self.idfile = self.config.get('id-file', '.noseids')
        self.ids = {}
        self.tests = {}
        if not os.path.isabs(self.idfile):
            # FIXME expand-user?
            self.idfile = os.path.join(os.getcwd(), self.idfile) = 0
        self._loaded = False

    def nextId(self): += 1

    def startTest(self, event):
        testid ='\n')[0]
        if testid not in self.tests:
            id_ = self.nextId()
            self.ids[id_] = testid
            self.tests[testid] = id_
            id_ = self.tests[testid]
        event.message('#%s ' % id_, (2,))

    def loadTestsFromName(self, event):
        """Implement loading of tests from name.
        If the name is a number, it might be an ID assigned by us. If we can
        find a test to which we have assigned that ID, is changed to
        the test's real ID. In this way, tests can be referred to via sequential
        id_ = self.argToId(
        if id_ is None:

        if id_ in self.ids:
            # Translate to test's real ID
   = self.ids[id_]

    def loadTestsFromNames(self, event):
        new_names = []
        for name in event.names:
            if self.argToId(name) in self.ids:
        event.names[:] = new_names

    def stopTestRun(self, event):
        fh = open(self.idfile, 'w')
        pickle.dump({'ids': self.ids, 'tests': self.tests}, fh)

    def argToId(self, name):
        """Try to translate name to numeric ID."""
        m = self.idpat.match(name)
        if m:
            return int(m.groups()[0])

    def loadIds(self):
        """Load previously pickled 'ids' and 'tests' attributes."""
        if self._loaded:

            fh = open(self.idfile, 'r')
        except EnvironmentError:
            # XXX: Should we set _loaded to True?
            data = pickle.load(fh)

        if 'ids' in data:
            self.ids = data['ids']
        if 'tests' in data:
            self.tests = data['tests'] = max(self.ids.keys())
        self._loaded = True
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.