summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/doctest.py4
-rw-r--r--Lib/test/test_doctest.py8
-rw-r--r--Lib/test/test_doctest3.txt5
3 files changed, 17 insertions, 0 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py
index 0a13d77..2708fc7 100644
--- a/Lib/doctest.py
+++ b/Lib/doctest.py
@@ -2328,6 +2328,8 @@ def DocFileTest(path, module_relative=True, package=None,
globs=None, parser=DocTestParser(), **options):
if globs is None:
globs = {}
+ else:
+ globs = globs.copy()
if package and not module_relative:
raise ValueError("Package may only be specified for module-"
@@ -2337,6 +2339,8 @@ def DocFileTest(path, module_relative=True, package=None,
if module_relative:
package = _normalize_module(package)
path = _module_relative_path(package, path)
+ if "__file__" not in globs:
+ globs["__file__"] = path
# Find the file and read it.
name = os.path.basename(path)
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
index 0ae6b8d..fe4d863 100644
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -2010,6 +2010,14 @@ def test_DocFileSuite():
modified the test globals. The test globals are
automatically cleared for us after a test.
+ Tests in a file run using `DocFileSuite` can also access the
+ `__file__` global, which is set to the name of the file
+ containing the tests:
+
+ >>> suite = doctest.DocFileSuite('test_doctest3.txt')
+ >>> suite.run(unittest.TestResult())
+ <unittest.TestResult run=1 errors=0 failures=0>
+
"""
def test_trailing_space_in_test():
diff --git a/Lib/test/test_doctest3.txt b/Lib/test/test_doctest3.txt
new file mode 100644
index 0000000..54a96d5
--- /dev/null
+++ b/Lib/test/test_doctest3.txt
@@ -0,0 +1,5 @@
+
+Here we check that `__file__` is provided:
+
+ >>> type(__file__)
+ <type 'str'>