gpt4 book ai didi

python doctest默认命名空间

转载 作者:太空狗 更新时间:2023-10-29 21:50:48 26 4
gpt4 key购买 nike

在我的模块的文档测试中,我想用完整的命名空间引用我的模块,例如:

  hp.myfunc(1)

我想通过以下方式避免使 doctests 困惑:

  import healpy as hp

在每个 doctest 中。

如果我运行 doctest.testmod,我知道我可以使用 globs 关键字来提供它,而如果我运行 nose,我可以使用 setup 函数。

是否有另一种可以同时使用两者的标准方法?

最佳答案

你是如何运行 doctests 的(没有 Nose ,就是说)?如果您在尝试运行它们时被 cd 进入包目录,您将遇到问题(如果您正在进行完全导入,那就是)。

我能够使用 nosetests 和内置的 doctest runner 运行一个简单的 doctest(具有完全合格的导入)。这是我的设置:

项目结构:

.
└── mypackage
├── __init__.py
└── mod.py

这是我的“mod.py”文件的内容:

"""foo() providing module

Example:
>>> import mypackage.mod
>>> mypackage.mod.foo()
'bar'
"""

def foo():
return "bar"

来自 '.'目录(项目根目录),我现在可以运行测试了:

$ python -m doctest -v mypackage/*.py
1 items had no tests:
__init__
0 tests in 1 items.
0 passed and 0 failed.
Test passed.
Trying:
import mypackage.mod
Expecting nothing
ok
Trying:
mypackage.mod.foo()
Expecting:
'bar'
ok
1 items had no tests:
mod.foo
1 items passed all tests:
2 tests in mod
2 tests in 2 items.
2 passed and 0 failed.
Test passed.

现在是 Nose 测试:

$ nosetests --with-doctest
.
----------------------------------------------------------------------
Ran 1 test in 0.008s

OK

如果我尝试从“mypackage”目录中运行 doctest,我会收到一个错误(我怀疑这是您的情况)。

最后,我认为这不会有什么不同,但我正在运行 Python 2.7.2

关于python doctest默认命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7682114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com