当测试文件的名称为__main__.py
时,doctest失败。这是预期的行为吗?
结果是您随后在__main__.py
中将没有任何文档测试。那么,解决方法是否只是将所有经过文档测试的内容移出__main__.py
并移至另一个模块中,并导入__main__.py
中?
要重现:制作一个内容简单的文件
"""
>>> 1+2
3
"""
将该文件命名为__main__.py
以外的其他文件
python -m doctest anything_but_main.py
和doctest顺利通过,没有任何问题。
命名该文件__main__.py
,
python -m doctest __main__.py
现在doctest崩溃很严重:
**********************************************************************
File "C:\Users\PERSON\AppData\Local\Continuum\anaconda3\envs\fle\lib\doctest.py", line 1812, in __main__.DebugRunner
Failed example:
runner.run(test)
Expected:
Traceback (most recent call last):
...
doctest.UnexpectedException: <DocTest foo from foo.py:0 (2 examples)>
Got:
Traceback (most recent call last):
File "C:\Users\PERSON\AppData\Local\Continuum\anaconda3\envs\fle\lib\doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest __main__.DebugRunner[15]>", line 1, in <module>
runner.run(test)
File "C:\Users\PERSON\AppData\Local\Continuum\anaconda3\envs\fle\lib\doctest.py", line 1836, in run
r = DocTestRunner.run(self, test, compileflags, out, False)
File "C:\Users\PERSON\AppData\Local\Continuum\anaconda3\envs\fle\lib\doctest.py", line 1475, in run
return self.__run(test, compileflags, out)
File "C:\Users\PERSON\AppData\Local\Continuum\anaconda3\envs\fle\lib\doctest.py", line 1381, in __run
exception)
File "C:\Users\PERSON\AppData\Local\Continuum\anaconda3\envs\fle\lib\doctest.py", line 1842, in report_unexpected_exception
raise UnexpectedException(test, example, exc_info)
UnexpectedException: <DocTest foo from foo.py:0 (2 examples)>
**********************************************************************
1 items had failures:
1 of 21 in __main__.DebugRunner
***Test Failed*** 1 failures.