7

我已经开始将文档测试集成到我的模块中。(万岁!)这些往往是作为脚本开始的文件,现在是 CLI 应用程序中的一些函数__name__=='__main__',所以我不想把测试的运行放在那里。我试过nosetests --with-doctest了,但是遇到了很多我不想看到的失败,因为在测试发现期间,这个导入模块不包含 doctests 但确实需要导入我没有在这个系统上安装的东西,或者应该在特殊的范围内运行蟒蛇安装。有没有办法可以运行我所有的文档测试?

我已经考虑在 vim 中使用热键来运行“import doctest; doctest.testfile(currentFilename)”来在当前模块中运行我的 doctest,以及另一个运行所有测试的脚本 - 其他 doctest 用户会做什么?还是我应该使用 doctest 以外的东西?

4

2 回答 2

6

您还可以创建包装所需 doctests 模块的单元测试,它是 doctests 的原生功能:http: //docs.python.org/2/library/doctest.html#unittest-api

import unittest
import doctest 
import my_module_with_doctests

def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite(my_module_with_doctests))
    return tests
于 2013-07-25T20:53:25.753 回答
3

我认为鼻子是方法。您应该明确排除有问题的模块,-e或者使用以下结构捕获代码中缺少的导入:

try:
    import simplejson as json
except ImportError:
    import json

更新:

另一种选择是为缺失的模块提供模拟替换。假设您的代码具有以下内容:

import myfunkymodule

并且您正在尝试在myfunkymodule缺少的系统中运行测试。您可以创建一个mock_modules/myfunkymodule.py文件,其中包含您需要的东西的模拟实现(也许使用MiniMock,如果您使用的是doctest ,我强烈建议您使用它)。然后你可以nose像这样运行:

$ PYTHONPATH=path_to/mock_modules nosetests --with-doctest
于 2010-09-06T19:00:53.383 回答