Exemple #1
0
def _choose_tagged_tests(tests, tags, mode='include'):
    """
    Select tests that are tagged/not tagged with at least one of the given tags.
    Set mode to 'include' to include the tests with tags, or 'exclude' to
    exclude the tests with the tags.
    """
    selected = []
    tags = set(tags)
    for test in _flatten_suite(tests):
        assert isinstance(test, unittest.TestCase)
        func = getattr(test, test._testMethodName)
        try:
            # Look up the method's underlying function (Python 2)
            func = func.im_func
        except AttributeError:
            pass

        found_tags = getattr(func, 'tags', None)
        # only include the test if the tags *are* present
        if mode == 'include':
            if found_tags is not None and found_tags & tags:
                selected.append(test)
        elif mode == 'exclude':
            # only include the test if the tags *are not* present
            if found_tags is None or not (found_tags & tags):
                selected.append(test)
        else:
            raise ValueError("Invalid 'mode' supplied: %s." % mode)
    return unittest.TestSuite(selected)
Exemple #2
0
    def _do_discovery(self, argv, Loader=None):
        """Upstream _do_discovery doesn't find our load_tests() functions."""

        loader = TestLoader() if Loader is None else Loader()
        topdir = abspath(dirname(dirname(__file__)))
        tests = loader.discover(join(topdir, 'numba/tests'), '*.py', topdir)
        self.test = unittest.TestSuite(tests)
Exemple #3
0
def _choose_random_tests(tests, ratio, seed):
    """
    Choose a given proportion of tests at random.
    """
    rnd = random.Random()
    rnd.seed(seed)
    if isinstance(tests, unittest.TestSuite):
        tests = _flatten_suite(tests)
    tests = rnd.sample(tests, int(len(tests) * ratio))
    tests = sorted(tests, key=lambda case: case.id())
    return unittest.TestSuite(tests)
Exemple #4
0
def load_testsuite(loader, dir):
    """Find tests in 'dir'."""
    suite = unittest.TestSuite()
    files = []
    for f in os.listdir(dir):
        path = join(dir, f)
        if isfile(path) and fnmatch(f, 'test_*.py'):
            files.append(f)
        elif isfile(join(path, '__init__.py')):
            suite.addTests(loader.discover(path))
    for f in files:
        # turn 'f' into a filename relative to the toplevel dir...
        f = relpath(join(dir, f), loader._top_level_dir)
        # ...and translate it to a module name.
        f = splitext(normpath(f.replace(os.path.sep, '.')))[0]
        suite.addTests(loader.loadTestsFromName(f))
    return suite
Exemple #5
0
def _choose_gitdiff_tests(tests):
    try:
        from git import Repo
    except ImportError:
        raise ValueError("gitpython needed for git functionality")
    repo = Repo('.')
    path = os.path.join('numba', 'tests')
    target = 'origin/master..HEAD'
    gdiff_paths = repo.git.diff(target, path, name_only=True).split()
    selected = []
    if PYVERSION > (2, 7): # inspect output changes in py3
        gdiff_paths = [os.path.join(repo.working_dir, x) for x in gdiff_paths]
    for test in _flatten_suite(tests):
        assert isinstance(test, unittest.TestCase)
        fname = inspect.getsourcefile(test.__class__)
        if fname in gdiff_paths:
            selected.append(test)
    print("Git diff identified %s tests" % len(selected))
    return unittest.TestSuite(selected)
Exemple #6
0
def _choose_tagged_tests(tests, tags):
    """
    Select tests that are tagged with at least one of the given tags.
    """
    selected = []
    tags = set(tags)
    for test in _flatten_suite(tests):
        assert isinstance(test, unittest.TestCase)
        func = getattr(test, test._testMethodName)
        try:
            # Look up the method's underlying function (Python 2)
            func = func.im_func
        except AttributeError:
            pass
        try:
            if func.tags & tags:
                selected.append(test)
        except AttributeError:
            # Test method doesn't have any tags
            pass
    return unittest.TestSuite(selected)
Exemple #7
0
 def run(self, test):
     run = _flatten_suite(test)[self.useslice]
     run.sort(key=test_mtime)
     wrapped = unittest.TestSuite(run)
     return super(BasicTestRunner, self).run(wrapped)