2010-10-04 56 views
1

这可能是一个愚蠢的问题。Python doctest示例失败

我与蟒蛇文档测试实验,我尝试运行this例如

if __name__ == "__main__": 
    import doctest 
    doctest.testfile("example.txt") 

我已经把“example.txt中”在同一文件夹中包含该示例的源文件结束代码,但我得到了以下错误:

Traceback (most recent call last): 
File "test_av_funktioner.py", line 61, in <module> 
doctest.testfile("example.txt") 
File "C:\Python26\lib\doctest.py", line 1947, in testfile 
text, filename = _load_testfile(filename, package, module_relative) 
File "C:\Python26\lib\doctest.py", line 219, in _load_testfile 
return open(filename).read(), filename 
IOError: [Errno 2] No such file or directory: 'example.txt' 

我能以某种方式告诉/组,其中文档测试模块搜索指定的文件吗?

回答

3

Doctest默认搜索相对于调用模块的目录(但您可以覆盖此)。

引述文档为doctest.testfile

Optional argument module_relative specifies how the filename should be interpreted:

  • If module_relative is True (the default), then filename specifies an OS-independent module-relative path. By default, this path is relative to the calling module’s directory; but if the package argument is specified, then it is relative to that package. To ensure OS-independence, filename should use / characters to separate path segments, and may not be an absolute path (i.e., it may not begin with /).
  • If module_relative is False, then filename specifies an OS-specific path. The path may be absolute or relative; relative paths are resolved with respect to the current working directory.