配置 Django 以查找所有模块中的所有文档测试?
如果我运行以下命令:
>python manage.py test
Django 会查看我的应用程序中的tests.py,并运行该文件中的任何文档测试或单元测试。它还查看 __ test __ 字典以运行额外的测试。因此,我可以从其他模块链接文档测试,如下所示:
#tests.py
from myapp.module1 import _function1, _function2
__test__ = {
"_function1": _function1,
"_function2": _function2
}
如果我想包含更多文档测试,是否有比在本字典中枚举它们更简单的方法?理想情况下,我只想让 Django 找到 myapp 应用程序中所有模块中的所有文档测试。
是否有某种反射黑客可以让我到达我想要的地方?
If I run the following command:
>python manage.py test
Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from other modules like so:
#tests.py
from myapp.module1 import _function1, _function2
__test__ = {
"_function1": _function1,
"_function2": _function2
}
If I want to include more doctests, is there an easier way than enumerating them all in this dictionary? Ideally, I just want to have Django find all doctests in all modules in the myapp application.
Is there some kind of reflection hack that would get me where I want to be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不久前自己解决了这个问题:
这使我可以将每个模块的测试放在自己的 test.py 文件中,这样它们就不会与我的应用程序代码的其余部分混淆。修改它很容易,只需在每个模块中查找文档测试并在找到它们时运行它们。
I solved this for myself a while ago:
This allowed me to put per-module tests in their own test.py file, so they didn't get mixed up with the rest of my application code. It would be easy to modify this to just look for doc tests in each of your modules and run them if it found them.
使用django-nose,因为nose会自动递归地查找所有测试。
Use django-nose since nose automatically find all tests recursivelly.
我不太了解 Djano 的测试速度,但据我了解,它使用自动 unittest discovery,就像 python -m unittest discovery 和 Nose 一样。
如果是这样,只需将以下文件放在发现可以找到的地方(通常只需将其命名为
test_doctest.py
或类似名称即可)。将
your_package
更改为要测试的包。所有模块(包括子包)都将进行文档测试。I'm not up to speed on Djano's testing, but as I understand it uses automatic unittest discovery, just like
python -m unittest discover
and Nose.If so, just put the following file somewhere the discovery will find it (usually just a matter of naming it
test_doctest.py
or similar).Change
your_package
to the package to test. All modules (including subpackages) will be doctested.以下是解决方案的关键要素:
tests.py:
要添加递归,请使用 os.walk() 遍历模块树并查找 python 包。
Here're key elements of solution:
tests.py:
To add recursion use
os.walk()
to traverse module tree and find python packages.感谢亚历克斯和保罗。这就是我想出的:
Thanks to Alex and Paul. This is what I came up with: