view.py 中的 Django 文档测试
Django 1.4 测试文档指出:
对于给定的 Django 应用程序,测试运行程序在两个位置查找文档测试:
models.py
文件。您可以定义模块级文档测试和/或单个模型的文档测试。通常的做法是将应用程序级文档测试放在模块文档字符串中,将模型级文档测试放在模型文档字符串中。应用程序目录(即保存 models.py 的目录)中名为
tests.py
的文件。该文件是您想要编写的任何和所有文档测试的挂钩,这些文档测试不一定与模型相关。
出于好奇,我想知道为什么 Django 的测试运行程序仅限于 models.py
中的文档测试,但更实际的是,我想知道如何扩展测试运行程序的文档测试以包含(例如) 运行manage.py test
时的views.py
和其他模块。
如果有任何意见,我将不胜感激。
谢谢。
布莱恩
The Django 1.4 documentation on tests states:
For a given Django application, the test runner looks for doctests in two places:
The
models.py
file. You can define module-level doctests and/or a doctest for individual models. It's common practice to put application-level doctests in the module docstring and model-level doctests in the model docstrings.A file called
tests.py
in the application directory -- i.e., the directory that holds models.py. This file is a hook for any and all doctests you want to write that aren't necessarily related to models.
Out of curiosity I'd like to know why Django's testrunner is limited to the doctests in models.py
, but more practically I'd like to know how one could expand the testrunner's doctests to include (for example) views.py
and other modules when running manage.py test
.
I'd be grateful for any input.
Thank you.
Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是我的
tests/__init__.py
实现,基于 Jesse Shieh 回答:基本上,这个解决方案允许向测试套件添加任意“文件”(实际上是模块)。它允许将单元测试拆分为单独的文件,并允许添加任何包含文档测试的模块。只需将模块名称添加到该文件顶部的相应列表中即可。
This is my
tests/__init__.py
implementation, based on Jesse Shieh answer:Basically, this solution allows adding arbitrary "files" (actually, modules) to the test suite. It allows splitting the unit tests into separate files, and allows adding any module that contains doctests. Just add the module names to the appropriate list at the top of this file.
将nosetests与django插件(django-sane-testing或django-nose)一起使用,并使用 --with-doctest 标志。
Use nosetests with plugin for django (django-sane-testing or django-nose) and use the --with-doctest flag.
我发布了一个 github gist,它允许您在项目中的任何文件或模块中运行测试。
从特定模块和文件运行文档测试
I posted a github gist that lets you run test in any file or module in your project.
Running doctests from specific modules and files
您可以尝试编写自己的
testrunner
并查看是否可以包含其他文件来检查文档测试。http://docs.djangoproject.com/en /dev/topics/testing/#defining-a-test-runner
You could try to write your own
testrunner
and see, if you can include other files to be checked for doc tests.http://docs.djangoproject.com/en/dev/topics/testing/#defining-a-test-runner
Django 的原生测试系统基于
unittest
包。所以它并没有那么强大。我建议您使用向后兼容的nose
unittest< /代码> 类固醇。将其与使用nose的Django测试运行器一起使用。您可以通过多种方式自定义鼻子,包括使用
-m
标志将其指向自定义测试位置。Django's native testing system is based on
unittest
package. So it is not as powerful as it can be.I recommend you using nose that is backward-compatible
unittest
on steroids. Use it along with Django test runner that uses nose. You can customize nose in many ways including pointing it to custom test locations using-m
flag.您可以通过在 tests.py 中添加/编辑 suite() 函数来完成此操作,该函数定义 django 测试运行程序将运行哪些测试。
然后像往常一样运行你的测试,你应该在views.py 中看到你的文档测试运行。
django 测试文档对此进行了更详细的描述
但是,请记住,构建您自己的测试套件意味着 django 测试运行程序不会自动运行您在tests.py 中的任何测试。例如,您必须手动将它们添加到您的套件中
You can do this by adding/editing the suite() function in tests.py which defines what tests will be run by the django test runner.
Then just run your tests as usual and you should see your doctests in views.py run.
This is described in more detail in the django testing documentation
However, keep in mind that constructing your own test suite means that the django test runner will not automatically run any tests you have in tests.py. You'll have to add these into your suite manually, for example
Django 1.6 中发生了变化:
所以我自己要做的就是(在 my_app/tests.py 中):
Things have changed in Django 1.6:
So all I had to do myself was (in my_app/tests.py):