本文介绍了doctest环境问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我没有在以下方面取得进展,并感谢任何 帮助。 这是一个解释性的Python会话。 /> .... Traceback(最近一次调用最后一次): 文件"< stdin>",第1行,< module> NameError:名称''f''未定义 假设我想对此行为进行doctest测试。我将上面的 剪切并粘贴到文件test.txt中。然后运行: python -c" import doctest; doctest.testfile(''test.txt'')" 这给了我意想不到的测试失败: python -c" import doctest; doctest.testfile(''test.txt'')" **************************** ********************** ******************** 文件test.txt,第5行,在test.txt中 失败的例子: delattr(this_module,''f'') 异常提出: Traceback(最近一次调用最后一次): 文件" /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5 / doctest.py",第1212行,在__run compileflags,1)in test.globs 文件"< doctest test .txt [3]>",第1行,在< module> delattr(this_module,''f'') AttributeError:f ********************************************* ***** ******************** 文件" test.txt",第6行,在test.txt中/> 失败的例子: f() 预计: 回溯(最近一次调用最后一次): 文件"< stdin>",第1行,< module> NameError:未定义名称''f'' 一无所获 ****************** ******************************** ****************** ** 1项失败: 2 of 5 in test.txt ***测试失败*** 2失败。 解决方案 ... Trace返回(最近一次调用最后一次): 文件"< stdin>",第1行,在< module>中 NameError:name''f''未定义 假设我想对此行为进行doctest测试。我将上面的 剪切并粘贴到文件test.txt中。然后运行: python -c" import doctest; doctest.testfile(''test.txt'')" 这给了我意想不到的测试失败: python -c" import doctest; doctest.testfile(''test.txt'')" **************************** ********************** ******************** 文件test.txt,第5行,在test.txt中 失败的例子: delattr(this_module,''f'') 异常提出: Traceback(最近一次调用最后一次): 文件" /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5 / doctest.py",第1212行,在__run compileflags,1)in test.globs 文件"< doctest test .txt [3]>",第1行,在< module> delattr(this_module,''f'') AttributeError:f ********************************************* ***** ******************** 文件" test.txt",第6行,在test.txt中/> 失败的例子: f() 预计: 回溯(最近一次调用最后一次): 档案 < stdin>",第1行,在< module>中 NameError:名称''f''未定义 一无所获 ************************************************ ** ******************** 1项失败: of 2 in test.txt ***测试失败*** 2失败。 doctest代码在没有__name__的模块中执行,似乎。 不幸的是(在这种情况下)内置模块用作a backback 帮助自己的名字: ''__ main__'' ''__builtin__'' 该怎么办? doctest可以更改为在带有名称和sys.modules条目的 模块中执行代码片段,虽然我没有看到很多好处 这里。 Peter ''__ main__'' ''__ builtin__'' 该怎么办? doctest可以更改为在带有名称和sys.modules条目的 模块中执行代码片段,虽然我没有看到很多好处 这里。 彼得,谢谢快速反应,但我不太明白 你在说什么。我不想改变doctest - 我想找到一个 的方法让我的例子使用doctest传递。 doctest.testfile自带很多参数,我怀疑如果我知道如何做到这一点我可以通过正确的参数来使这个 示例工作。这不是我真正想要记录/测试的,它是一个 最小的例子来证明这个问题。 再次感谢。 ''__ main__'' '__ builtin__'' 该怎么办? doctest可以更改为在具有名称和sys.modules条目的模块中执行代码片段,虽然我在这里看不到多少好处。 彼得,谢谢快速反应,但我不太明白 你在说什么。我不想改变doctest - 我想找到一个 的方式让我的例子使用doctest传递。 doctest提供的环境类似于交互式 解释器但不完全相同。特别是没有意义 __name__。 这里有两种选择: (1) .... Traceback(最近一次调用最后一次): 文件"< stdin>" ,第1行,在< module> NameError:名称''f''未定义 (2) .... Traceback(最近一次调用最后一次): 文件"< stdin>",第1行,< module> NameError:名称''f''未定义 如果这些都不起作用你必须提供更多的背景。 彼得 I''m not making progress with the following and would appreciate anyhelp. Here''s an interpreted Python session. .... Traceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name ''f'' is not defined Suppose I want to doctest this behaviour. I cut and paste the aboveinto a file "test.txt" then run: python -c "import doctest; doctest.testfile(''test.txt'')" This gives me unexpected test failures: python -c "import doctest; doctest.testfile(''test.txt'')"************************************************** ********************File "test.txt", line 5, in test.txtFailed example:delattr(this_module, ''f'')Exception raised:Traceback (most recent call last):File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/doctest.py", line 1212, in __runcompileflags, 1) in test.globsFile "<doctest test.txt[3]>", line 1, in <module>delattr(this_module, ''f'')AttributeError: f************************************************** ********************File "test.txt", line 6, in test.txtFailed example:f()Expected:Traceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name ''f'' is not definedGot nothing************************************************** ********************1 items had failures:2 of 5 in test.txt***Test Failed*** 2 failures. 解决方案 ... Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name ''f'' is not definedSuppose I want to doctest this behaviour. I cut and paste the aboveinto a file "test.txt" then run:python -c "import doctest; doctest.testfile(''test.txt'')"This gives me unexpected test failures:python -c "import doctest; doctest.testfile(''test.txt'')"************************************************** ********************File "test.txt", line 5, in test.txtFailed example: delattr(this_module, ''f'')Exception raised: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/doctest.py", line 1212, in __run compileflags, 1) in test.globs File "<doctest test.txt[3]>", line 1, in <module> delattr(this_module, ''f'') AttributeError: f************************************************** ********************File "test.txt", line 6, in test.txtFailed example: f()Expected: Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name ''f'' is not definedGot nothing************************************************** ********************1 items had failures: 2 of 5 in test.txt***Test Failed*** 2 failures.The doctest code is executed in a module without a __name__, it seems.Unfortunately (in this case) the builtin module serves as a fallbackhelping out with its own name: ''__main__'' ''__builtin__''What to do about it? doctest could be changed to execute code snippets in amodule with a name and a sys.modules entry though I don''t see much benefithere. Peter ''__main__'' ''__builtin__''What to do about it? doctest could be changed to execute code snippets in amodule with a name and a sys.modules entry though I don''t see much benefithere.Peter, thanks for the quick response, but I don''t quite understandwhat you''re saying. I don''t want to change doctest -- I want to find away to make my example pass using doctest. doctest.testfile comes with lots of parameters, and I suspect if Iknew how to do it I could pass in the correct parameters to make thisexample work. It''s not what I actually want to document/test, it''s aminimal example which demonstrates the problem. Thanks again. ''__main__'' ''__builtin__''What to do about it? doctest could be changed to execute code snippets ina module with a name and a sys.modules entry though I don''t see muchbenefit here. Peter, thanks for the quick response, but I don''t quite understandwhat you''re saying. I don''t want to change doctest -- I want to find away to make my example pass using doctest.The environment that doctest provides is similar to the interactiveinterpreter but not identical. In particular there is no meaningful__name__. Here are two alternatives: (1) .... Traceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name ''f'' is not defined (2) .... Traceback (most recent call last):File "<stdin>", line 1, in <module>NameError: name ''f'' is not defined If these don''t work you''ll have to give a bit more context. Peter 这篇关于doctest环境问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 20:28