Beispiel #1
0
def ic_testmod(m,
               name=None,
               globs=None,
               verbose=None,
               report=True,
               optionflags=0,
               extraglobs=None,
               raise_on_error=False,
               exclude_empty=False):
    """See original code in doctest.testmod."""
    if name is None:
        name = m.__name__
    finder = DocTestFinder(exclude_empty=exclude_empty)
    if raise_on_error:
        runner = DebugRunner(checker=Py23DocChecker(),
                             verbose=verbose,
                             optionflags=optionflags)
    else:
        runner = DocTestRunner(checker=Py23DocChecker(),
                               verbose=verbose,
                               optionflags=optionflags)
    for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
        runner.run(test)

    if report:
        runner.summarize()

    return doctest.TestResults(runner.failures, runner.tries)
Beispiel #2
0
    def get_doc_test_cases_from_module(self, name):
        mod = self.import_dotted_name(name)

        finder = DocTestFinder()
        tests = finder.find(mod)

        doc_test_cases = []
        for test in tests:
            doc_test_cases.append(self.make_doc_test_case(test))
        return doc_test_cases
Beispiel #3
0
def discover(module='__main__',
             suite=None,
             *,
             doctest=doctest,
             unittest=unittest,
             loader=loader):
    from doctest import DocTestCase, DocTestFinder
    if isinstance(module, str):
        module = __import__('importlib').import_module(module)
    if suite is None:
        return Sweet(module=module,
                     doctest=doctest,
                     unittest=unittest,
                     loader=loader)

    for name, object in vars(module).items():
        if getmodule(object) == module or module.__name__ == 'interactive':
            if unittest:
                if isinstance(object, type) and issubclass(object, TestCase):
                    suite.addTests(loader.loadTestsFromTestCase(object)._tests)

                if callable(object) and not isinstance(object,
                                                       (partial, type)):
                    function_test_case = infer(object)
                    if function_test_case:
                        suite.addTest(function_test_case)

            if doctest and hasattr(object, '__name__'):
                suite.addTests(map(DocTestCase, DocTestFinder().find(object)))
    return suite
Beispiel #4
0
def test_testil():
    # nose test generator
    import testil
    from doctest import DocTestFinder, DocTestRunner

    def run(test):
        result = runner.run(test)
        assert not result.failed, (
            "%s of %s examples failed while testing %s"
            % (result.failed, len(test.examples), test.name))

    # Find, parse, and run all tests in this module.
    finder = DocTestFinder()
    runner = DocTestRunner()
    for test in finder.find(testil, "testil"):
        yield run, test
Beispiel #5
0
 def tester(modules, runner, f=None):
     '''Runs tests for each module.'''
     results = []
     for module in modules:
         tests = DocTestFinder().find(module)
         tests.sort(key=lambda test: test.name)
         for test in tests:
             if test.examples == []:    # Skip empty tests
                 pass
             else:
                 if f:
                     f.write('\n'.join(('-' * 80, test.name, '-' * 80, '\n')))
                     results.append(runner.run(test, out=f.write))
                 else:
                     print('\n'.join(('-' * 80, test.name, '-' * 80)))
                     results.append(runner.run(test))
     return results
Beispiel #6
0
def make_full_suite():
    from doctest import DocTestFinder, DocTestSuite
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    finder = DocTestFinder()
    for modname in (__name__, "pylibmc", "_pylibmc"):
        ss = (DocTestSuite(sys.modules[modname], test_finder=finder),
              loader.loadTestsFromName(modname))
        for subsuite in ss:
            map(suite.addTest, subsuite._tests)
    return suite
Beispiel #7
0
    def __init__(self, verbose=False):
        """New decorator.

        Parameters
        ----------

        verbose : boolean, optional (False)
          Passed to the doctest finder and runner to control verbosity.
        """
        self.verbose = verbose
        # We can reuse the same finder for all instances
        self.finder = DocTestFinder(verbose=verbose, recurse=False)
Beispiel #8
0
def check_doctest(func_name, module, run=True):
    """Check that MODULE.FUNC_NAME doctest passes."""
    func = getattr(module, func_name)
    tests = DocTestFinder().find(func)
    if not tests:
        print("No doctests found for " + func_name)
        return True
    fn = lambda: DocTestRunner().run(tests[0])
    result = test_eval(fn, tuple())
    if result.failed != 0:
        print("A doctest example failed for " + func_name + ".")
        return True
    return False
Beispiel #9
0
def check_doctest(func_name, module, run=True): # func_name是函数的名字,module是模块的名字
    """Check that MODULE.FUNC_NAME doctest passes."""
    func = getattr(module, func_name) # 好吧,终于看到了这个玩意了。Java的反射机制是吧!
    tests = DocTestFinder().find(func)
    if not tests:
        print("No doctests found for " + func_name)
        return True
    fn = lambda: DocTestRunner().run(tests[0]) # 一个匿名函数
    result = test_eval(fn, tuple()) #
    if result.failed != 0:
        print("A doctest example failed for " + func_name + ".")
        return True
    return False
Beispiel #10
0
 def tester(modules, runner):
     """Runs tests for each module."""
     results = []
     for module in modules:
         try:
             tests = DocTestFinder().find(module)
             tests.sort(key=lambda test: test.name)
         except ValueError:
             tests = []
         for test in tests:
             if mock:
                 results.append(None)
             elif test.examples == []:    # Skip empty tests
                 pass
             elif log != False:
                 f = log.handlers[0].stream
                 f.write('\n'.join(('-' * 80, test.name, '-' * 80, '\n')))
                 results.append(runner.run(test, out=f))
             else:
                 print('\n'.join(('-' * 80, test.name, '-' * 80)))
                 results.append(runner.run(test))
     return results
Beispiel #11
0
    def __init__(cls, name, bases, dct):
        super(DocMetaTestCase, cls).__init__(name, bases, dct)

        module = dct['module']
        globs = dct.get('globs', None)
        extraglobs = dct.get('extraglobs', None)

        if isinstance(module, basestring):
            # transform a module name into a module
            module = sys.modules[module]

        for doctest in DocTestFinder(recurse=True).find(module,
                                                        name='test_doc',
                                                        globs=globs,
                                                        extraglobs=extraglobs):
            cls.add_test(doctest)
Beispiel #12
0
    def __init__(cls, name, bases, dct):
        super(DocMetaTestCase, cls).__init__(name, bases, dct)

        try:
            module = dct['module']
        except KeyError:
            if dct.get('__test__', True) == False:
                # This is some kind of abstract class. Do nothing.
                return
            else:
                raise ValueError('No module was given for doctest search!')

        globs = dct.get('globs', None)
        extraglobs = dct.get('extraglobs', None)

        if isinstance(module, basestring):
            # transform a module name into a module
            module = sys.modules[module]

        for doctest in DocTestFinder(recurse=True).find(module, name='test_doc', globs=globs, extraglobs=extraglobs):
            cls.add_test(doctest)
Beispiel #13
0
def check_doctests(modules=None, respect_skips=True, verbose=True):
    """Check whether doctests can be run as-is without errors.

    Parameters
    ----------
    modules : dict, optional
        (module name -> module) mapping of submodules defined in a
        package as returned by ``discover_modules()``. If omitted,
        ``discover_modules()`` will be called for ``pyvista``.

    respect_skips : bool, optional
        Whether to ignore doctest examples that contain a DOCTEST:+SKIP
        directive.

    verbose : bool, optional
        Whether to print passes/failures as the testing progresses.
        Failures are printed at the end in every case.

    Returns
    -------
    failures : dict of (Exception, str)  tuples
        An (object name -> (exception raised, failing code)) mapping
        of failed doctests under the specified modules.

    """
    skip_pattern = re.compile('doctest: *\+SKIP')

    if modules is None:
        modules = discover_modules()

    # find and parse all docstrings; this will also remove any duplicates
    doctests = {
        dt.name: dt
        for module_name, module in modules.items()
        for dt in DocTestFinder(recurse=True).find(module, globs={})
    }

    # loop over doctests in alphabetical order for sanity
    sorted_names = sorted(doctests)
    failures = {}
    for dt_name in sorted_names:
        dt = doctests[dt_name]
        if not dt.examples:
            continue

        # mock print to suppress output from a few talkative tests
        globs = {'print': (lambda *args, **kwargs:...)}
        for iline, example in enumerate(dt.examples, start=1):
            if (not example.source.strip() or
                (respect_skips and skip_pattern.search(example.source))):
                continue
            try:
                exec(example.source, globs)
            except Exception as exc:
                if verbose:
                    print(f'FAILED: {dt.name} -- {repr(exc)}')
                erroring_code = ''.join(
                    [example.source for example in dt.examples[:iline]])
                failures[dt_name] = exc, erroring_code
                break
        else:
            if verbose:
                print(f'PASSED: {dt.name}')

    total = len(doctests)
    fails = len(failures)
    passes = total - fails
    print(f'\n{passes} passes and {fails} failures '
          f'out of {total} total doctests.\n')
    if not fails:
        return failures

    print('List of failures:')
    for name, (exc, erroring_code) in failures.items():
        print('-' * 60)
        print(f'{name}:')
        print(indent(erroring_code, '    '))
        print(repr(exc))
    print('-' * 60)

    return failures
            else:
                return want, got

        def check_output(self, want, got, optionflags):
            want, got = self.remove_u(want, got)
            return super(Py23DocChecker, self).check_output(
                want, got, optionflags)

        def output_difference(self, example, got, optionflags):
            example.want, got = self.remove_u(example.want, got)
            return super(Py23DocChecker, self).output_difference(
                example, got, optionflags)

    global master
    m = sys.modules.get('__main__')
    finder = DocTestFinder()
    runner = DocTestRunner(checker=Py23DocChecker())
    for test in finder.find(m, m.__name__):
        runner.run(test)
    runner.summarize()
    import sys
    sys.exit()

    # yapf: disable
    class Cache(object):
        def __init__(self):
            self.cache = {}

        def set(self, k, v, ttl):
            self.cache[k] = v
Beispiel #15
0
        else:
            last_line_was_a_doc_test = True


def call_and_return_none_on_exception(func, *args, **kwargs):
    try:
        return func(*args, **kwargs)
    except Exception:
        return None


import inspect
from doctest import DocTestFinder
from inspect import signature, getfile, getcomments, getsource, getsourcelines, getdoc

doctest_finder = DocTestFinder(recurse=False)


def argnames(func):
    return ' '.join(inspect.signature(func).parameters)


def tokenize_for_code(string):
    camelcase_and_underscore_tokenizer(string)


_func_info_funcs = {
    'func_name': lambda f: f.__qualname__,
    'arg_names': argnames,
    'comments': getcomments,
    'doc': lambda f: '\n'.join(non_doctest_lines(getdoc(f))),