Python doctest.DocTestParser() Examples
The following are 30
code examples of doctest.DocTestParser().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
doctest
, or try the search function
.
Example #1
Source File: test_tutorials.py From sqlalchemy with MIT License | 7 votes |
def _run_doctest_for_content(self, name, content): optionflags = ( doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE | doctest.IGNORE_EXCEPTION_DETAIL | _get_allow_unicode_flag() ) runner = doctest.DocTestRunner( verbose=None, optionflags=optionflags, checker=_get_unicode_checker(), ) globs = {"print_function": print_function} parser = doctest.DocTestParser() test = parser.get_doctest(content, globs, name, name, 0) runner.run(test) runner.summarize() assert not runner.failures
Example #2
Source File: test_result.py From green with MIT License | 6 votes |
def test_doctest(self): """ If we parse a doctest, we get all the fields we need. """ test = """ >>> f() 42 """ def f(): return 42 parser = doctest.DocTestParser() dt = parser.get_doctest(test, {"f": f}, "doctest.name", "somefile.py", 20) dt.__module__ = "somefile" p = proto_test(doctest.DocTestCase(dt)) # short description self.assertEqual(p.getDescription(2), "doctest.name") # long description description = p.getDescription(3) self.assertIn("doctest.name", description) self.assertIn("somefile.py", description) self.assertIn("20", description) # dotted name self.assertEqual(p.dotted_name, "doctest.name")
Example #3
Source File: doctest.py From python-netsurv with MIT License | 6 votes |
def collect(self): import doctest # inspired by doctest.testfile; ideally we would use it directly, # but it doesn't support passing a custom checker encoding = self.config.getini("doctest_encoding") text = self.fspath.read_text(encoding) filename = str(self.fspath) name = self.fspath.basename globs = {"__name__": "__main__"} optionflags = get_optionflags(self) runner = _get_runner( verbose=0, optionflags=optionflags, checker=_get_checker(), continue_on_failure=_get_continue_on_failure(self.config), ) parser = doctest.DocTestParser() test = parser.get_doctest(text, globs, name, filename, 0) if test.examples: yield DoctestItem(test.name, self, runner, test)
Example #4
Source File: doctest.py From python-netsurv with MIT License | 6 votes |
def collect(self): import doctest # inspired by doctest.testfile; ideally we would use it directly, # but it doesn't support passing a custom checker encoding = self.config.getini("doctest_encoding") text = self.fspath.read_text(encoding) filename = str(self.fspath) name = self.fspath.basename globs = {"__name__": "__main__"} optionflags = get_optionflags(self) runner = _get_runner( verbose=0, optionflags=optionflags, checker=_get_checker(), continue_on_failure=_get_continue_on_failure(self.config), ) parser = doctest.DocTestParser() test = parser.get_doctest(text, globs, name, filename, 0) if test.examples: yield DoctestItem(test.name, self, runner, test)
Example #5
Source File: documenter.py From schedula with European Union Public License 1.1 | 6 votes |
def _import_docstring(documenter): code_content = _import_docstring_code_content(documenter) if code_content: # noinspection PyBroadException try: code, content = code_content parser = DocTestParser() runner = DocTestRunner(verbose=0, optionflags=NORMALIZE_WHITESPACE | ELLIPSIS) glob = {} if documenter.modname: exec('from %s import *\n' % documenter.modname, glob) tests = parser.get_doctest(code, glob, '', '', 0) runner.run(tests, clear_globs=False) documenter.object = tests.globs[documenter.name] documenter.code = content documenter.is_doctest = True return True except Exception: pass
Example #6
Source File: _test.py From guildai with Apache License 2.0 | 5 votes |
def run_test_file_with_config(filename, globs, optionflags): """Modified from doctest.py to use custom checker.""" text, filename = _load_testfile(filename) name = os.path.basename(filename) if globs is None: globs = {} else: globs = globs.copy() if '__name__' not in globs: globs['__name__'] = '__main__' checker = Py23DocChecker() runner = TestRunner(checker=checker, verbose=None, optionflags=optionflags) parser = doctest.DocTestParser() test = parser.get_doctest(text, globs, name, filename, 0) flags = ( print_function.compiler_flag | absolute_import.compiler_flag | division.compiler_flag ) runner.run(test, flags) results = runner.summarize() if doctest.master is None: doctest.master = runner else: doctest.master.merge(runner) return results
Example #7
Source File: noseclasses.py From ImageFusion with MIT License | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #8
Source File: test_matchers.py From pth-toolkit with BSD 2-Clause "Simplified" License | 5 votes |
def run_doctest(obj, name): p = doctest.DocTestParser() t = p.get_doctest( obj.__doc__, sys.modules[obj.__module__].__dict__, name, '', 0) r = doctest.DocTestRunner() output = StringIO() r.run(t, out=output.write) return r.failures, output.getvalue()
Example #9
Source File: noseclasses.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #10
Source File: reqs.py From trains with Apache License 2.0 | 5 votes |
def _parse_docstring(node): """Extract code from docstring.""" docstring = ast.get_docstring(node) if docstring: parser = doctest.DocTestParser() try: dt = parser.get_doctest(docstring, {}, None, None, None) except ValueError: # >>> 'abc' pass else: examples = dt.examples return '\n'.join([example.source for example in examples]) return None
Example #11
Source File: shapefile.py From restapi with GNU General Public License v2.0 | 5 votes |
def test(**kwargs): import doctest doctest.NORMALIZE_WHITESPACE = 1 verbosity = kwargs.get('verbose', 0) if verbosity == 0: print('Running doctests...') # ignore py2-3 unicode differences import re class Py23DocChecker(doctest.OutputChecker): def check_output(self, want, got, optionflags): if sys.version_info[0] == 2: got = re.sub("u'(.*?)'", "'\\1'", got) got = re.sub('u"(.*?)"', '"\\1"', got) res = doctest.OutputChecker.check_output(self, want, got, optionflags) return res def summarize(self): doctest.OutputChecker.summarize(True) # run tests runner = doctest.DocTestRunner(checker=Py23DocChecker(), verbose=verbosity) with open("README.md","rb") as fobj: test = doctest.DocTestParser().get_doctest(string=fobj.read().decode("utf8").replace('\r\n','\n'), globs={}, name="README", filename="README.md", lineno=0) failure_count, test_count = runner.run(test) # print results if verbosity: runner.summarize(True) else: if failure_count == 0: print('All test passed successfully') elif failure_count > 0: runner.summarize(verbosity) return failure_count
Example #12
Source File: test_doctest.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_unicode(): """ Check doctest with a non-ascii filename: >>> doc = ''' ... >>> raise Exception('clé') ... ''' ... >>> parser = doctest.DocTestParser() >>> test = parser.get_doctest(doc, {}, "foo-bär@baz", "foo-bär@baz.py", 0) >>> test <DocTest foo-bär@baz from foo-bär@baz.py:0 (1 example)> >>> runner = doctest.DocTestRunner(verbose=False) >>> runner.run(test) # doctest: +ELLIPSIS ********************************************************************** File "foo-bär@baz.py", line 2, in foo-bär@baz Failed example: raise Exception('clé') Exception raised: Traceback (most recent call last): File ... compileflags, 1), test.globs) File "<doctest foo-bär@baz[0]>", line 1, in <module> raise Exception('clé') Exception: clé TestResults(failed=1, attempted=1) """
Example #13
Source File: shapefile.py From pyshp with MIT License | 5 votes |
def test(**kwargs): import doctest doctest.NORMALIZE_WHITESPACE = 1 verbosity = kwargs.get('verbose', 0) if verbosity == 0: print('Running doctests...') # ignore py2-3 unicode differences import re class Py23DocChecker(doctest.OutputChecker): def check_output(self, want, got, optionflags): if sys.version_info[0] == 2: got = re.sub("u'(.*?)'", "'\\1'", got) got = re.sub('u"(.*?)"', '"\\1"', got) res = doctest.OutputChecker.check_output(self, want, got, optionflags) return res def summarize(self): doctest.OutputChecker.summarize(True) # run tests runner = doctest.DocTestRunner(checker=Py23DocChecker(), verbose=verbosity) with open("README.md","rb") as fobj: test = doctest.DocTestParser().get_doctest(string=fobj.read().decode("utf8").replace('\r\n','\n'), globs={}, name="README", filename="README.md", lineno=0) failure_count, test_count = runner.run(test) # print results if verbosity: runner.summarize(True) else: if failure_count == 0: print('All test passed successfully') elif failure_count > 0: runner.summarize(verbosity) return failure_count
Example #14
Source File: noseclasses.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #15
Source File: testdocs.py From PyCRS with MIT License | 5 votes |
def test(**kwargs): doctest.NORMALIZE_WHITESPACE = 1 verbosity = kwargs.get('verbose', 0) if verbosity == 0: print('Running doctests...') # ignore py2-3 unicode differences import re class Py23DocChecker(doctest.OutputChecker): def check_output(self, want, got, optionflags): if sys.version_info[0] == 2: got = re.sub("u'(.*?)'", "'\\1'", got) got = re.sub('u"(.*?)"', '"\\1"', got) res = doctest.OutputChecker.check_output(self, want, got, optionflags) return res def summarize(self): doctest.OutputChecker.summarize(True) # run tests runner = doctest.DocTestRunner(checker=Py23DocChecker(), verbose=verbosity) with open('README.md') as r: doc = r.read() test = doctest.DocTestParser().get_doctest(string=doc, globs={}, name="__init__", filename="__init__.py", lineno=0) failure_count, test_count = runner.run(test) # print results if verbosity: runner.summarize(True) else: if failure_count == 0: print('All test passed successfully') elif failure_count > 0: runner.summarize(verbosity) return failure_count
Example #16
Source File: ok.py From Gofer-Grader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def run_doctest(name, doctest_string, global_environment): """ Run a single test with given global_environment. Returns (True, '') if the doctest passes. Returns (False, failure_message) if the doctest fails. """ examples = doctest.DocTestParser().parse( doctest_string, name ) test = doctest.DocTest( [e for e in examples if isinstance(e, doctest.Example)], global_environment, name, None, None, doctest_string ) doctestrunner = doctest.DocTestRunner(verbose=True) runresults = io.StringIO() with redirect_stdout(runresults), redirect_stderr(runresults), hide_outputs(): doctestrunner.run(test, clear_globs=False) with open(os.devnull, 'w') as f, redirect_stderr(f), redirect_stdout(f): result = doctestrunner.summarize(verbose=True) # An individual test can only pass or fail if result.failed == 0: return (True, '') else: return False, runresults.getvalue()
Example #17
Source File: noseclasses.py From recruit with Apache License 2.0 | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #18
Source File: parser.py From pigar with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _parse_docstring(node): """Extract code from docstring.""" docstring = ast.get_docstring(node) if docstring: parser = doctest.DocTestParser() try: dt = parser.get_doctest(docstring, {}, None, None, None) except ValueError: # >>> 'abc' pass else: examples = dt.examples return '\n'.join([example.source for example in examples]) return None
Example #19
Source File: noseclasses.py From coffeegrindsize with MIT License | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #20
Source File: testing.py From ok-client with Apache License 2.0 | 5 votes |
def __init__(self, args, assignment): super().__init__(args, assignment) # The environment in which the doctests are run (global vars) self.good_env = {} self.verb = self.args.verbose # Initialize the doctest module objects that will do the testing/parse self.parser = DocTestParser() self.runner = DocTestRunner(verbose=self.verb, checker=DebugOutputChecker(), optionflags=FAIL_FAST) self.lines_exec = 0 self.lines_total = 0
Example #21
Source File: noseclasses.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #22
Source File: doctest_nose_plugin.py From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
def loadTestsFromFileUnicode(self, filename): if self.extension and anyp(filename.endswith, self.extension): name = os.path.basename(filename) dh = codecs.open(filename, 'r', self.options.get('doctestencoding')) try: doc = dh.read() finally: dh.close() fixture_context = None globs = {'__file__': filename} if self.fixtures: base, ext = os.path.splitext(name) dirname = os.path.dirname(filename) sys.path.append(dirname) fixt_mod = base + self.fixtures try: fixture_context = __import__(fixt_mod, globals(), locals(), ["nop"]) except ImportError as e: log.debug("Could not import %s: %s (%s)", fixt_mod, e, sys.path) log.debug("Fixture module %s resolved to %s", fixt_mod, fixture_context) if hasattr(fixture_context, 'globs'): globs = fixture_context.globs(globs) parser = doctest.DocTestParser() test = parser.get_doctest( doc, globs=globs, name=name, filename=filename, lineno=0 ) if test.examples: case = DocFileCase( test, optionflags=self.optionflags, setUp=getattr(fixture_context, 'setup_test', None), tearDown=getattr(fixture_context, 'teardown_test', None), result_var=self.doctest_result_var, ) if fixture_context: yield ContextList((case,), context=fixture_context) else: yield case else: yield False # no tests to load
Example #23
Source File: test_doctest.py From android_universal with MIT License | 5 votes |
def test_unicode(): """ Check doctest with a non-ascii filename: >>> doc = ''' ... >>> raise Exception('clé') ... ''' ... >>> parser = doctest.DocTestParser() >>> test = parser.get_doctest(doc, {}, "foo-bär@baz", "foo-bär@baz.py", 0) >>> test <DocTest foo-bär@baz from foo-bär@baz.py:0 (1 example)> >>> runner = doctest.DocTestRunner(verbose=False) >>> runner.run(test) # doctest: +ELLIPSIS ********************************************************************** File "foo-bär@baz.py", line 2, in foo-bär@baz Failed example: raise Exception('clé') Exception raised: Traceback (most recent call last): File ... compileflags, 1), test.globs) File "<doctest foo-bär@baz[0]>", line 1, in <module> raise Exception('clé') Exception: clé TestResults(failed=1, attempted=1) """
Example #24
Source File: noseclasses.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #25
Source File: gofer.py From otter-grader with BSD 3-Clause "New" or "Revised" License | 5 votes |
def run_doctest(name, doctest_string, global_environment): """ Run a single test with given global_environment. Returns (True, '') if the doctest passes. Returns (False, failure_message) if the doctest fails. Args: name (str): Name of doctest doctest_string (str): Doctest in string form global_environment (dict of str: str): Global environment resulting from the execution of a python script/notebook Returns: (bool, str): Results from running the test """ examples = doctest.DocTestParser().parse( doctest_string, name ) test = doctest.DocTest( [e for e in examples if isinstance(e, doctest.Example)], global_environment, name, None, None, doctest_string ) doctestrunner = doctest.DocTestRunner(verbose=True) runresults = io.StringIO() with redirect_stdout(runresults), redirect_stderr(runresults), hide_outputs(): doctestrunner.run(test, clear_globs=False) with open(os.devnull, 'w') as f, redirect_stderr(f), redirect_stdout(f): result = doctestrunner.summarize(verbose=True) # An individual test can only pass or fail if result.failed == 0: return (True, '') else: return False, runresults.getvalue()
Example #26
Source File: shapefile.py From BlenderGIS with GNU General Public License v3.0 | 5 votes |
def test(**kwargs): import doctest doctest.NORMALIZE_WHITESPACE = 1 verbosity = kwargs.get('verbose', 0) if verbosity == 0: print('Running doctests...') # ignore py2-3 unicode differences import re class Py23DocChecker(doctest.OutputChecker): def check_output(self, want, got, optionflags): if sys.version_info[0] == 2: got = re.sub("u'(.*?)'", "'\\1'", got) got = re.sub('u"(.*?)"', '"\\1"', got) res = doctest.OutputChecker.check_output(self, want, got, optionflags) return res def summarize(self): doctest.OutputChecker.summarize(True) # run tests runner = doctest.DocTestRunner(checker=Py23DocChecker(), verbose=verbosity) with open("README.md","rb") as fobj: test = doctest.DocTestParser().get_doctest(string=fobj.read().decode("utf8").replace('\r\n','\n'), globs={}, name="README", filename="README.md", lineno=0) failure_count, test_count = runner.run(test) # print results if verbosity: runner.summarize(True) else: if failure_count == 0: print('All test passed successfully') elif failure_count > 0: runner.summarize(verbosity) return failure_count
Example #27
Source File: noseclasses.py From twitter-stock-recommendation with MIT License | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #28
Source File: noseclasses.py From keras-lambda with MIT License | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #29
Source File: noseclasses.py From mxnet-lambda with Apache License 2.0 | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']
Example #30
Source File: noseclasses.py From Computable with MIT License | 5 votes |
def configure(self, options, config): # parent method sets enabled flag from command line --with-numpydoctest Plugin.configure(self, options, config) self.finder = self.test_finder_class() self.parser = doctest.DocTestParser() if self.enabled: # Pull standard doctest out of plugin list; there's no reason to run # both. In practice the Unplugger plugin above would cover us when # run from a standard numpy.test() call; this is just in case # someone wants to run our plugin outside the numpy.test() machinery config.plugins.plugins = [p for p in config.plugins.plugins if p.name != 'doctest']