是否可以使用unit2运行文档测试

发布于 2024-09-11 17:39:53 字数 120 浏览 0 评论 0原文

我最近从鼻子切换到新的unittest2包来满足我的Python单元测试需求。它做了我想做的一切,除了我无法得到它的“发现”命令来识别我的代码中的文档测试 - 我仍然必须使用鼻子来运行它们。这是没有实现还是我在这里遗漏了什么?

I recently switched from nose to the new unittest2 package for my python unit testing needs. It does everything I want, except from the fact that I can't get its "discover" command to recognize the doctests in my code - I still have to use nose to run them. Is this not implemented or is there something I'm missing here?

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

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

发布评论

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

评论(2

小鸟爱天空丶 2024-09-18 17:39:53

Unit2 仅发现常规 Python 测试。为了让它运行您的文档测试,恐怕您需要编写一些最小的样板文件。另外:即将推出的插件架构将使其中一些任务的自动化变得容易。

同时。你可能想看看tox(由unittest2创建者在此处描述)http: //www.voidspace.org.uk/python/weblog/arch_d7_2010_07_10.shtml

Unit2 only discovers regular Python tests. In order to have it run your doctests, I'm afraid you would need to write some minimal boilerplate. Also: the upcoming plugin architecture will make it easy to automate some of these tasks.

In the meantime. you might want to take a look at tox (described here by unittest2 creator) http://www.voidspace.org.uk/python/weblog/arch_d7_2010_07_10.shtml

梦亿 2024-09-18 17:39:53

告诉unit2你的doctests所需的样板实际上在当前的doctest文档中给出,尽管我花了几分钟才找到它:

http://docs.python.org/library/doctest.html#unittest-api

请注意,您可以将模块名称传递给 DocTestSuite构造函数,而不必自己导入模块,这可以将样板文件的长度减少一半;它只需要看起来像:

from doctest import DocTestSuite
from unittest import TestSuite

def load_tests(loader, tests, pattern):
    suite = TestSuite()
    suite.addTests(DocTestSuite('my.module.one'))
    suite.addTests(DocTestSuite('my.module.two'))
    suite.addTests(DocTestSuite('my.module.three'))
    return suite

The boilerplate needed to tell unit2 about your doctests is actually given in the current doctest documentation, though it took me a few minutes to find it:

http://docs.python.org/library/doctest.html#unittest-api

Note that you can pass module names to the DocTestSuite constructor instead of having to import the module yourself, which can cut the length of your boilerplate file in half; it just needs to look like:

from doctest import DocTestSuite
from unittest import TestSuite

def load_tests(loader, tests, pattern):
    suite = TestSuite()
    suite.addTests(DocTestSuite('my.module.one'))
    suite.addTests(DocTestSuite('my.module.two'))
    suite.addTests(DocTestSuite('my.module.three'))
    return suite
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文