mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Remove deprecated Tester class from doctest module.
This commit is contained in:
parent
bf086a1eb3
commit
3183585a86
3 changed files with 16 additions and 215 deletions
|
@ -163,10 +163,9 @@ prohibit it by passing ``verbose=False``. In either of those cases,
|
||||||
``sys.argv`` is not examined by :func:`testmod` (so passing :option:`-v` or not
|
``sys.argv`` is not examined by :func:`testmod` (so passing :option:`-v` or not
|
||||||
has no effect).
|
has no effect).
|
||||||
|
|
||||||
Since Python 2.6, there is also a command line shortcut for running
|
There is also a command line shortcut for running :func:`testmod`. You can
|
||||||
:func:`testmod`. You can instruct the Python interpreter to run the doctest
|
instruct the Python interpreter to run the doctest module directly from the
|
||||||
module directly from the standard library and pass the module name(s) on the
|
standard library and pass the module name(s) on the command line::
|
||||||
command line::
|
|
||||||
|
|
||||||
python -m doctest -v example.py
|
python -m doctest -v example.py
|
||||||
|
|
||||||
|
@ -233,10 +232,9 @@ Like :func:`testmod`, :func:`testfile`'s verbosity can be set with the
|
||||||
:option:`-v` command-line switch or with the optional keyword argument
|
:option:`-v` command-line switch or with the optional keyword argument
|
||||||
*verbose*.
|
*verbose*.
|
||||||
|
|
||||||
Since Python 2.6, there is also a command line shortcut for running
|
There is also a command line shortcut for running :func:`testfile`. You can
|
||||||
:func:`testfile`. You can instruct the Python interpreter to run the doctest
|
instruct the Python interpreter to run the doctest module directly from the
|
||||||
module directly from the standard library and pass the file name(s) on the
|
standard library and pass the file name(s) on the command line::
|
||||||
command line::
|
|
||||||
|
|
||||||
python -m doctest -v example.txt
|
python -m doctest -v example.txt
|
||||||
|
|
||||||
|
@ -888,15 +886,10 @@ Unittest API
|
||||||
------------
|
------------
|
||||||
|
|
||||||
As your collection of doctest'ed modules grows, you'll want a way to run all
|
As your collection of doctest'ed modules grows, you'll want a way to run all
|
||||||
their doctests systematically. Prior to Python 2.4, :mod:`doctest` had a barely
|
their doctests systematically. :mod:`doctest` provides two functions that can
|
||||||
documented :class:`Tester` class that supplied a rudimentary way to combine
|
be used to create :mod:`unittest` test suites from modules and text files
|
||||||
doctests from multiple modules. :class:`Tester` was feeble, and in practice most
|
containing doctests. These test suites can then be run using :mod:`unittest`
|
||||||
serious Python testing frameworks build on the :mod:`unittest` module, which
|
test runners::
|
||||||
supplies many flexible ways to combine tests from multiple sources. So, in
|
|
||||||
Python 2.4, :mod:`doctest`'s :class:`Tester` class is deprecated, and
|
|
||||||
:mod:`doctest` provides two functions that can be used to create :mod:`unittest`
|
|
||||||
test suites from modules and text files containing doctests. These test suites
|
|
||||||
can then be run using :mod:`unittest` test runners::
|
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import doctest
|
import doctest
|
||||||
|
|
|
@ -166,10 +166,9 @@ ELLIPSIS_MARKER = '...'
|
||||||
# 4. DocTest Finder -- extracts test cases from objects
|
# 4. DocTest Finder -- extracts test cases from objects
|
||||||
# 5. DocTest Runner -- runs test cases
|
# 5. DocTest Runner -- runs test cases
|
||||||
# 6. Test Functions -- convenient wrappers for testing
|
# 6. Test Functions -- convenient wrappers for testing
|
||||||
# 7. Tester Class -- for backwards compatibility
|
# 7. Unittest Support
|
||||||
# 8. Unittest Support
|
# 8. Debugging Support
|
||||||
# 9. Debugging Support
|
# 9. Example Usage
|
||||||
# 10. Example Usage
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
## 1. Utility Functions
|
## 1. Utility Functions
|
||||||
|
@ -1968,72 +1967,7 @@ def run_docstring_examples(f, globs, verbose=False, name="NoName",
|
||||||
runner.run(test, compileflags=compileflags)
|
runner.run(test, compileflags=compileflags)
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
## 7. Tester
|
## 7. Unittest Support
|
||||||
######################################################################
|
|
||||||
# This is provided only for backwards compatibility. It's not
|
|
||||||
# actually used in any way.
|
|
||||||
|
|
||||||
class Tester:
|
|
||||||
def __init__(self, mod=None, globs=None, verbose=None, optionflags=0):
|
|
||||||
|
|
||||||
warnings.warn("class Tester is deprecated; "
|
|
||||||
"use class doctest.DocTestRunner instead",
|
|
||||||
DeprecationWarning, stacklevel=2)
|
|
||||||
if mod is None and globs is None:
|
|
||||||
raise TypeError("Tester.__init__: must specify mod or globs")
|
|
||||||
if mod is not None and not inspect.ismodule(mod):
|
|
||||||
raise TypeError("Tester.__init__: mod must be a module; %r" %
|
|
||||||
(mod,))
|
|
||||||
if globs is None:
|
|
||||||
globs = mod.__dict__
|
|
||||||
self.globs = globs
|
|
||||||
|
|
||||||
self.verbose = verbose
|
|
||||||
self.optionflags = optionflags
|
|
||||||
self.testfinder = DocTestFinder()
|
|
||||||
self.testrunner = DocTestRunner(verbose=verbose,
|
|
||||||
optionflags=optionflags)
|
|
||||||
|
|
||||||
def runstring(self, s, name):
|
|
||||||
test = DocTestParser().get_doctest(s, self.globs, name, None, None)
|
|
||||||
if self.verbose:
|
|
||||||
print("Running string", name)
|
|
||||||
(f,t) = self.testrunner.run(test)
|
|
||||||
if self.verbose:
|
|
||||||
print(f, "of", t, "examples failed in string", name)
|
|
||||||
return TestResults(f,t)
|
|
||||||
|
|
||||||
def rundoc(self, object, name=None, module=None):
|
|
||||||
f = t = 0
|
|
||||||
tests = self.testfinder.find(object, name, module=module,
|
|
||||||
globs=self.globs)
|
|
||||||
for test in tests:
|
|
||||||
(f2, t2) = self.testrunner.run(test)
|
|
||||||
(f,t) = (f+f2, t+t2)
|
|
||||||
return TestResults(f,t)
|
|
||||||
|
|
||||||
def rundict(self, d, name, module=None):
|
|
||||||
import types
|
|
||||||
m = types.ModuleType(name)
|
|
||||||
m.__dict__.update(d)
|
|
||||||
if module is None:
|
|
||||||
module = False
|
|
||||||
return self.rundoc(m, name, module)
|
|
||||||
|
|
||||||
def run__test__(self, d, name):
|
|
||||||
import types
|
|
||||||
m = types.ModuleType(name)
|
|
||||||
m.__test__ = d
|
|
||||||
return self.rundoc(m, name)
|
|
||||||
|
|
||||||
def summarize(self, verbose=None):
|
|
||||||
return self.testrunner.summarize(verbose)
|
|
||||||
|
|
||||||
def merge(self, other):
|
|
||||||
self.testrunner.merge(other.testrunner)
|
|
||||||
|
|
||||||
######################################################################
|
|
||||||
## 8. Unittest Support
|
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
_unittest_reportflags = 0
|
_unittest_reportflags = 0
|
||||||
|
@ -2393,7 +2327,7 @@ def DocFileSuite(*paths, **kw):
|
||||||
return suite
|
return suite
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
## 9. Debugging Support
|
## 8. Debugging Support
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
def script_from_examples(s):
|
def script_from_examples(s):
|
||||||
|
@ -2546,7 +2480,7 @@ def debug(module, name, pm=False):
|
||||||
debug_script(testsrc, pm, module.__dict__)
|
debug_script(testsrc, pm, module.__dict__)
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
## 10. Example Usage
|
## 9. Example Usage
|
||||||
######################################################################
|
######################################################################
|
||||||
class _TestClass:
|
class _TestClass:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -2285,132 +2285,6 @@ using the optional keyword argument `encoding`:
|
||||||
>>> doctest.master = None # Reset master.
|
>>> doctest.master = None # Reset master.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
# Ignore all warnings about the use of class Tester in this module.
|
|
||||||
# Note that the name of this module may differ depending on how it's
|
|
||||||
# imported, so the use of __name__ is important.
|
|
||||||
warnings.filterwarnings("ignore", "class Tester", DeprecationWarning,
|
|
||||||
__name__, 0)
|
|
||||||
|
|
||||||
def old_test1(): r"""
|
|
||||||
>>> from doctest import Tester
|
|
||||||
>>> t = Tester(globs={'x': 42}, verbose=0)
|
|
||||||
>>> t.runstring(r'''
|
|
||||||
... >>> x = x * 2
|
|
||||||
... >>> print(x)
|
|
||||||
... 42
|
|
||||||
... ''', 'XYZ')
|
|
||||||
**********************************************************************
|
|
||||||
Line 3, in XYZ
|
|
||||||
Failed example:
|
|
||||||
print(x)
|
|
||||||
Expected:
|
|
||||||
42
|
|
||||||
Got:
|
|
||||||
84
|
|
||||||
TestResults(failed=1, attempted=2)
|
|
||||||
>>> t.runstring(">>> x = x * 2\n>>> print(x)\n84\n", 'example2')
|
|
||||||
TestResults(failed=0, attempted=2)
|
|
||||||
>>> t.summarize()
|
|
||||||
**********************************************************************
|
|
||||||
1 items had failures:
|
|
||||||
1 of 2 in XYZ
|
|
||||||
***Test Failed*** 1 failures.
|
|
||||||
TestResults(failed=1, attempted=4)
|
|
||||||
>>> t.summarize(verbose=1)
|
|
||||||
1 items passed all tests:
|
|
||||||
2 tests in example2
|
|
||||||
**********************************************************************
|
|
||||||
1 items had failures:
|
|
||||||
1 of 2 in XYZ
|
|
||||||
4 tests in 2 items.
|
|
||||||
3 passed and 1 failed.
|
|
||||||
***Test Failed*** 1 failures.
|
|
||||||
TestResults(failed=1, attempted=4)
|
|
||||||
"""
|
|
||||||
|
|
||||||
def old_test2(): r"""
|
|
||||||
>>> from doctest import Tester
|
|
||||||
>>> t = Tester(globs={}, verbose=1)
|
|
||||||
>>> test = r'''
|
|
||||||
... # just an example
|
|
||||||
... >>> x = 1 + 2
|
|
||||||
... >>> x
|
|
||||||
... 3
|
|
||||||
... '''
|
|
||||||
>>> t.runstring(test, "Example")
|
|
||||||
Running string Example
|
|
||||||
Trying:
|
|
||||||
x = 1 + 2
|
|
||||||
Expecting nothing
|
|
||||||
ok
|
|
||||||
Trying:
|
|
||||||
x
|
|
||||||
Expecting:
|
|
||||||
3
|
|
||||||
ok
|
|
||||||
0 of 2 examples failed in string Example
|
|
||||||
TestResults(failed=0, attempted=2)
|
|
||||||
"""
|
|
||||||
|
|
||||||
def old_test3(): r"""
|
|
||||||
>>> from doctest import Tester
|
|
||||||
>>> t = Tester(globs={}, verbose=0)
|
|
||||||
>>> def _f():
|
|
||||||
... '''Trivial docstring example.
|
|
||||||
... >>> assert 2 == 2
|
|
||||||
... '''
|
|
||||||
... return 32
|
|
||||||
...
|
|
||||||
>>> t.rundoc(_f) # expect 0 failures in 1 example
|
|
||||||
TestResults(failed=0, attempted=1)
|
|
||||||
"""
|
|
||||||
|
|
||||||
def old_test4(): """
|
|
||||||
>>> import types
|
|
||||||
>>> m1 = types.ModuleType('_m1')
|
|
||||||
>>> m2 = types.ModuleType('_m2')
|
|
||||||
>>> test_data = \"""
|
|
||||||
... def _f():
|
|
||||||
... '''>>> assert 1 == 1
|
|
||||||
... '''
|
|
||||||
... def g():
|
|
||||||
... '''>>> assert 2 != 1
|
|
||||||
... '''
|
|
||||||
... class H:
|
|
||||||
... '''>>> assert 2 > 1
|
|
||||||
... '''
|
|
||||||
... def bar(self):
|
|
||||||
... '''>>> assert 1 < 2
|
|
||||||
... '''
|
|
||||||
... \"""
|
|
||||||
>>> exec(test_data, m1.__dict__)
|
|
||||||
>>> exec(test_data, m2.__dict__)
|
|
||||||
>>> m1.__dict__.update({"f2": m2._f, "g2": m2.g, "h2": m2.H})
|
|
||||||
|
|
||||||
Tests that objects outside m1 are excluded:
|
|
||||||
|
|
||||||
>>> from doctest import Tester
|
|
||||||
>>> t = Tester(globs={}, verbose=0)
|
|
||||||
>>> t.rundict(m1.__dict__, "rundict_test", m1) # f2 and g2 and h2 skipped
|
|
||||||
TestResults(failed=0, attempted=4)
|
|
||||||
|
|
||||||
Once more, not excluding stuff outside m1:
|
|
||||||
|
|
||||||
>>> t = Tester(globs={}, verbose=0)
|
|
||||||
>>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped.
|
|
||||||
TestResults(failed=0, attempted=8)
|
|
||||||
|
|
||||||
The exclusion of objects from outside the designated module is
|
|
||||||
meant to be invoked automagically by testmod.
|
|
||||||
|
|
||||||
>>> doctest.testmod(m1, verbose=False)
|
|
||||||
TestResults(failed=0, attempted=4)
|
|
||||||
"""
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
## Main
|
## Main
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue