python doctest默认命名空间

发布于 2024-12-08 17:44:06 字数 308 浏览 0 评论 0原文

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

  hp.myfunc(1)

并且我想通过

  import healpy as hp

在每个文档测试中编写:来避免文档测试混乱。

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

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

In the doctests of my module I would like to reference my module with the full namespace, for example:

  hp.myfunc(1)

And I would like to avoid cluttering the doctests by writing:

  import healpy as hp

in each of the doctests.

if I run doctest.testmod, I know I can use the globs keyword to provide this, while if I run nose, I can use the setup function.

Is there another standard way that could work with both?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你的占有欲 2024-12-15 17:44:06

你如何运行文档测试(即没有鼻子)?如果您在尝试运行它们时通过 cd 进入包目录,您将遇到问题(如果您正在进行完全导入)。

我能够获得一个简单的 doctest(具有完全限定的导入),同时使用鼻子测试和内置的 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.

现在nosetests:

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

OK

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

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

How are you running the doctests (without nose, that is)? If you are cd'd into the package directory when you attempt to run them, you will run into problems (if you are doing a full import, that is).

I was able to get a simple doctest (with fully-qualified imports) running with both nosetests and the builtin doctest runner. Here's my setup:

Project structure:

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

Here are the contents of my 'mod.py' file:

"""foo() providing module

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

def foo():
    return "bar"

from the '.' directory (project root), I can now run tests:

$ 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.

And now the nosetests:

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

OK

If I try to run the doctest from within the 'mypackage' directory, I get an error (which is, I suspect, what's happening in your case).

Finally, I don't think this should make a difference, but I'm running Python 2.7.2

独闯女儿国 2024-12-15 17:44:06

我不知道鼻子,但您可以在 testmod()testfile() 中使用 globs 参数。

这是一个简单的模块(称为 foobar.py),请注意,我不导入 os:

#!/usr/bin/python
"""
    >>> os.pipe
    <built-in function pipe>
"""

您可以像这样测试该模块(控制台示例):

$ python2.7
Python 2.7.2 (default, Jun 29 2011, 11:10:00) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import doctest, foobar
2
>>> doctest.testmod(foobar)  ## will fail as expected because os has not been imported
**********************************************************************
File "foobar.py", line 2, in foobar
Failed example:
    os.pipe
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest foobar[0]>", line 1, in <module>
        os.pipe
    NameError: name 'os' is not defined
**********************************************************************
1 items had failures:
   1 of   1 in foobar
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=1)
>>> import os
>>> globs = {'os': os}
>>> doctest.testmod(foobar, globs=globs)
TestResults(failed=0, attempted=1)
>>> # Win :)

您的示例应该说:

globs = {'hp': healp}

I don't know about nose, but you can use the globs argument in testmod() and testfile().

Here's a simple module (called foobar.py), note that I do not import os:

#!/usr/bin/python
"""
    >>> os.pipe
    <built-in function pipe>
"""

You can test the module like this (console example):

$ python2.7
Python 2.7.2 (default, Jun 29 2011, 11:10:00) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import doctest, foobar
2
>>> doctest.testmod(foobar)  ## will fail as expected because os has not been imported
**********************************************************************
File "foobar.py", line 2, in foobar
Failed example:
    os.pipe
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest foobar[0]>", line 1, in <module>
        os.pipe
    NameError: name 'os' is not defined
**********************************************************************
1 items had failures:
   1 of   1 in foobar
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=1)
>>> import os
>>> globs = {'os': os}
>>> doctest.testmod(foobar, globs=globs)
TestResults(failed=0, attempted=1)
>>> # Win :)

Your example should say:

globs = {'hp': healp}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文