Python doctest.testfile() Examples

The following are 30 code examples of doctest.testfile(). 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: doctest.py    From python-netsurv with MIT License 6 votes vote down vote up
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 #2
Source File: doctest.py    From python-netsurv with MIT License 6 votes vote down vote up
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 #3
Source File: test_all.py    From Mastering-Object-Oriented-Python-Second-Edition with MIT License 6 votes vote down vote up
def run_doctest_suite(pkg_mod_iter: Iterable[Tuple[Path, Iterable[Path]]]) -> None:
    """Doctest each module individually. With a few exclusions.

    Might be simpler to use doctest.testfile()? However, the examples aren't laid out for this.
    """
    for package, module_iter in pkg_mod_iter:
        print()
        print(package.name)
        print("="*len(package.name))
        print()
        for module_path in module_iter:
            if module_path.stem in DOCTEST_EXCLUDE:
                print(f"Excluding {module_path}")
                continue
            result = subprocess.run(['python3', '-m', 'doctest', str(module_path)])
            if result.returncode != 0:
                sys.exit(f"Failure {result!r} in {module_path}") 
Example #4
Source File: tests_docs.py    From django-ca with GNU General Public License v3.0 5 votes vote down vote up
def test_python_intro(self):
        doctest.testfile('%s/python/intro.rst' % base, globs=self.get_globs()) 
Example #5
Source File: shapefile.py    From aws_taxi with MIT License 5 votes vote down vote up
def test():
    import doctest
    doctest.NORMALIZE_WHITESPACE = 1
    doctest.testfile("README.txt", verbose=1) 
Example #6
Source File: tests_docs.py    From django-ca with GNU General Public License v3.0 5 votes vote down vote up
def test_python_models(self):
        doctest.testfile('%s/python/models.rst' % base, globs=self.get_globs()) 
Example #7
Source File: mddoctest.py    From dependencies with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _main():
    markdown_files = glob("**/*.md", recursive=True)
    exit_code = 0
    for markdown_file in markdown_files:
        failed, attempted = testfile(markdown_file, module_relative=False)
        exit_code += failed
    exit(exit_code) 
Example #8
Source File: test_zipimport.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #9
Source File: test_zipimport.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #10
Source File: test_doctest.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_lineendings(): r"""
*nix systems use \n line endings, while Windows systems use \r\n.  Python
handles this using universal newline mode for reading files.  Let's make
sure doctest does so (issue 8473) by creating temporary test files using each
of the two line disciplines.  One of the two will be the "wrong" one for the
platform the test is run on.

Windows line endings first:

    >>> import tempfile, os
    >>> fn = tempfile.mktemp()
    >>> with open(fn, 'wb') as f:
    ...    f.write(b'Test:\r\n\r\n  >>> x = 1 + 1\r\n\r\nDone.\r\n')
    35
    >>> doctest.testfile(fn, module_relative=False, verbose=False)
    TestResults(failed=0, attempted=1)
    >>> os.remove(fn)

And now *nix line endings:

    >>> fn = tempfile.mktemp()
    >>> with open(fn, 'wb') as f:
    ...     f.write(b'Test:\n\n  >>> x = 1 + 1\n\nDone.\n')
    30
    >>> doctest.testfile(fn, module_relative=False, verbose=False)
    TestResults(failed=0, attempted=1)
    >>> os.remove(fn)

""" 
Example #11
Source File: test_zipimport.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #12
Source File: test_doc.py    From darkc0de-old-stuff with GNU General Public License v3.0 5 votes vote down vote up
def testDoc(filename, name=None):
    print "--- %s: Run tests" % filename
    failure, nb_test = testfile(
        filename, optionflags=ELLIPSIS, name=name)
    if failure:
        exit(1)
    print "--- %s: End of tests" % filename 
Example #13
Source File: test_doc.py    From portion with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_readme():
    failure = None

    try:
        doctest.testfile('../README.md', raise_on_error=True, globs={'P': P})
    except doctest.DocTestFailure as e:
        failure = e.example.want, e.got, e.example.source

    if failure:
        # Make pytest display it outside the "except" block, to avoid a noisy traceback
        want, got, example = failure
        assert want.strip() == got.strip(), 'DocTest failure in "{}"'.format(example.strip())
        assert False  # In case .strip() removed something useful 
Example #14
Source File: test_readme.py    From schedula with European Union Public License 1.1 5 votes vote down vote up
def runTest(self):
        import doctest
        failure_count, test_count = doctest.testfile(
            '../README.rst',
            optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
        )
        self.assertGreater(test_count, 0, (failure_count, test_count))
        self.assertEqual(failure_count, 0, (failure_count, test_count)) 
Example #15
Source File: test_zipimport.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #16
Source File: sentence_runner.py    From example-code with MIT License 5 votes vote down vote up
def test(cls, verbose=False):

    res = doctest.testfile(
            TEST_FILE,
            globs={'Sentence': cls},
            verbose=verbose,
            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
    tag = 'FAIL' if res.failed else 'OK'
    print(TEST_MSG.format(cls.__module__, res, tag)) 
Example #17
Source File: iso2709.py    From example-code with MIT License 5 votes vote down vote up
def test():
    import doctest
    doctest.testfile('iso2709_test.txt') 
Example #18
Source File: aritprog_runner.py    From example-code with MIT License 5 votes vote down vote up
def test(gen_factory, verbose=False):
    res = doctest.testfile(
            TEST_FILE,
            globs={'aritprog_gen': gen_factory},
            verbose=verbose,
            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
    tag = 'FAIL' if res.failed else 'OK'
    print(TEST_MSG.format(gen_factory.__module__, res, tag)) 
Example #19
Source File: tombola_runner.py    From example-code with MIT License 5 votes vote down vote up
def test(cls, verbose=False):

    res = doctest.testfile(
            TEST_FILE,
            globs={'ConcreteTombola': cls},  # <5>
            verbose=verbose,
            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
    tag = 'FAIL' if res.failed else 'OK'
    print(TEST_MSG.format(cls.__name__, res, tag))  # <6> 
Example #20
Source File: test_doc.py    From odl with Mozilla Public License 2.0 5 votes vote down vote up
def test_file(doc_src_file):
    # FIXXXME: This doesn't seem to actually test the file :-(
    doctest.testfile(doc_src_file, module_relative=False, report=True,
                     extraglobs=doctest_extraglobs, verbose=True,
                     optionflags=doctest_optionflags)
    plt.close('all') 
Example #21
Source File: test_zipimport.py    From android_universal with MIT License 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #22
Source File: test_doctest.py    From android_universal with MIT License 5 votes vote down vote up
def test_lineendings(): r"""
*nix systems use \n line endings, while Windows systems use \r\n.  Python
handles this using universal newline mode for reading files.  Let's make
sure doctest does so (issue 8473) by creating temporary test files using each
of the two line disciplines.  One of the two will be the "wrong" one for the
platform the test is run on.

Windows line endings first:

    >>> import tempfile, os
    >>> fn = tempfile.mktemp()
    >>> with open(fn, 'wb') as f:
    ...    f.write(b'Test:\r\n\r\n  >>> x = 1 + 1\r\n\r\nDone.\r\n')
    35
    >>> doctest.testfile(fn, module_relative=False, verbose=False)
    TestResults(failed=0, attempted=1)
    >>> os.remove(fn)

And now *nix line endings:

    >>> fn = tempfile.mktemp()
    >>> with open(fn, 'wb') as f:
    ...     f.write(b'Test:\n\n  >>> x = 1 + 1\n\nDone.\n')
    30
    >>> doctest.testfile(fn, module_relative=False, verbose=False)
    TestResults(failed=0, attempted=1)
    >>> os.remove(fn)

""" 
Example #23
Source File: test.py    From sqlobject with GNU Lesser General Public License v2.1 5 votes vote down vote up
def test():
    for doc in ['SQLObject.rst']:
        doctest.testfile(doc, optionflags=doctest.ELLIPSIS) 
Example #24
Source File: shapefile123.py    From BlenderGIS with GNU General Public License v3.0 5 votes vote down vote up
def test():
    import doctest
    doctest.NORMALIZE_WHITESPACE = 1
    doctest.testfile("README.txt", verbose=1) 
Example #25
Source File: test_zipimport.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #26
Source File: test_readme.py    From formulas with European Union Public License 1.1 5 votes vote down vote up
def runTest(self):
        failure_count, test_count = doctest.testfile(
            '../README.rst', optionflags=doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
        )
        self.assertGreater(test_count, 0, (failure_count, test_count))
        self.assertEqual(failure_count, 0, (failure_count, test_count)) 
Example #27
Source File: test_zipimport.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #28
Source File: test_zipimport.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def doDoctestFile(self, module):
        log = []
        old_master, doctest.master = doctest.master, None
        try:
            doctest.testfile(
                'xyz.txt', package=module, module_relative=True,
                globs=locals()
            )
        finally:
            doctest.master = old_master
        self.assertEqual(log,[True]) 
Example #29
Source File: test_doctest.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_lineendings(): r"""
*nix systems use \n line endings, while Windows systems use \r\n.  Python
handles this using universal newline mode for reading files.  Let's make
sure doctest does so (issue 8473) by creating temporary test files using each
of the two line disciplines.  One of the two will be the "wrong" one for the
platform the test is run on.

Windows line endings first:

    >>> import tempfile, os
    >>> fn = tempfile.mktemp()
    >>> with open(fn, 'wb') as f:
    ...     f.write('Test:\r\n\r\n  >>> x = 1 + 1\r\n\r\nDone.\r\n')
    >>> doctest.testfile(fn, module_relative=False, verbose=False)
    TestResults(failed=0, attempted=1)
    >>> os.remove(fn)

And now *nix line endings:

    >>> fn = tempfile.mktemp()
    >>> with open(fn, 'wb') as f:
    ...     f.write('Test:\n\n  >>> x = 1 + 1\n\nDone.\n')
    >>> doctest.testfile(fn, module_relative=False, verbose=False)
    TestResults(failed=0, attempted=1)
    >>> os.remove(fn)

"""

# old_test1, ... used to live in doctest.py, but cluttered it.  Note
# that these use the deprecated doctest.Tester, so should go away (or
# be rewritten) someday. 
Example #30
Source File: rundoctests.py    From cysignals with GNU Lesser General Public License v3.0 5 votes vote down vote up
def testfile(file):
    # Child process
    try:
        if sys.platform == 'darwin':
            from cysignals.signals import _setup_alt_stack
            _setup_alt_stack()
        failures, _ = doctest.testfile(file, module_relative=False, optionflags=flags, parser=parser)
        if not failures:
            os._exit(0)
    except BaseException as E:
        print(E)
    finally:
        os._exit(23)