对文件夹中的所有 python 模块运行我的所有文档测试,不会因为导入错误而出现失败
我已经开始将文档测试集成到我的模块中。 (万岁!)这些往往是作为脚本开始的文件,现在是 __name__=='__main__'
中 CLI 应用程序的一些函数,所以我不想将正在运行的那里的测试。我尝试了nosetests --with-doctest,但是遇到了很多我不想看到的失败,因为在测试发现过程中,这个导入模块不包含doctests,但确实需要导入我不需要的东西尚未安装在此系统上,或者应该在特殊的 python 安装中运行。有没有办法可以运行我所有的文档测试?
我考虑过在 vim 中使用一个热键来运行“import doctest; doctest.testfile(currentFilename)”来运行当前模块中的 doctest,以及运行所有测试的另一个脚本 - 其他 doctest 用户会做什么?或者我应该使用 doctest 以外的东西?
I've started integrating doctests into my modules. (Hooray!) These tend to be files which started as scripts, and are now are a few functions with CLI apps in the __name__=='__main__'
, so I don't want to put the running of the tests there. I tried nosetests --with-doctest
, but get lots of failures I don't want to see, because during test discovery this import modules which don't contain doctests but do require importing things I don't have installed on this system, or should be run within special python installations. Is there a way I can run just all of my doctests?
I've considered a hotkey in vim to run "import doctest; doctest.testfile(currentFilename)" to run my doctests in the current module, and another script that runs all the tests - what do other doctest users do? Or should I be using something other than doctest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以创建包装所需 doctests 模块的单元测试,这是 doctests 的本机功能: http://docs.python.org/2/library/doctest.html#unittest-api。
You can also create unittests that wrap desired doctests modules, it is a native feature of doctests: http://docs.python.org/2/library/doctest.html#unittest-api.
我认为 nose 就是这样。您应该使用
-e
明确排除有问题的模块,或者使用如下结构捕获代码中缺少的导入:更新:
另一种选择是为缺少的模块提供模拟替换模块。假设您的代码具有如下内容:
并且您尝试在缺少
myfunkymodule
的系统中运行测试。您可以创建一个mock_modules/myfunkymodule.py
文件,其中包含您需要的内容的模拟实现(也许使用 MiniMock,如果您使用doctest<,我强烈推荐它/a>)。然后你可以像这样运行nose
:I think nose is the way. You should either exclude the problematic modules explicitly with
-e
or catch the missing imports in your code with constructs like this:Update:
Another option is to provide mock replacements for the missing modules. Let's say your code has something like this:
and you're trying run your tests in a system where
myfunkymodule
is missing. You could create amock_modules/myfunkymodule.py
file with mock implementations of the stuff you need from it (perhaps using MiniMock, which I highly recommend if you are using doctest). You could then runnose
like this: