view.py 中的 Django 文档测试

发布于 2024-08-24 01:04:08 字数 641 浏览 8 评论 0原文

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 技术交流群。

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

发布评论

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

评论(7

清醇 2024-08-31 01:04:09

这是我的 tests/__init__.py 实现,基于 Jesse Shieh 回答

import doctest
import unittest

list_of_doctests = [
    'myapp.views.myview',
    'myapp.forms.myform',
]
list_of_unittests = [
    'sometestshere',  # This file is myapp/tests/sometestshere.py
    'moretestshere',  # This file is myapp/tests/moretestshere.py
    'myapp.tests.othertest',  # Absolute paths also work.
]

def suite():
    suite = unittest.TestSuite()
    for t in list_of_doctests:
        suite.addTest(doctest.DocTestSuite(
            __import__(t, globals(), locals(), fromlist=["*"])
        ))
    for t in list_of_unittests:
        suite.addTest(unittest.TestLoader().loadTestsFromModule(
            __import__(t, globals(), locals(), fromlist=["*"])
        ))
    return suite

基本上,这个解决方案允许向测试套件添加任意“文件”(实际上是模块)。它允许将单元测试拆分为单独的文件,并允许添加任何包含文档测试的模块。只需将模块名称添加到该文件顶部的相应列表中即可。

This is my tests/__init__.py implementation, based on Jesse Shieh answer:

import doctest
import unittest

list_of_doctests = [
    'myapp.views.myview',
    'myapp.forms.myform',
]
list_of_unittests = [
    'sometestshere',  # This file is myapp/tests/sometestshere.py
    'moretestshere',  # This file is myapp/tests/moretestshere.py
    'myapp.tests.othertest',  # Absolute paths also work.
]

def suite():
    suite = unittest.TestSuite()
    for t in list_of_doctests:
        suite.addTest(doctest.DocTestSuite(
            __import__(t, globals(), locals(), fromlist=["*"])
        ))
    for t in list_of_unittests:
        suite.addTest(unittest.TestLoader().loadTestsFromModule(
            __import__(t, globals(), locals(), fromlist=["*"])
        ))
    return suite

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.

再浓的妆也掩不了殇 2024-08-31 01:04:09

将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.

彩扇题诗 2024-08-31 01:04:09

我发布了一个 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

£冰雨忧蓝° 2024-08-31 01:04:09

您可以尝试编写自己的 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

盛夏已如深秋| 2024-08-31 01:04:09

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.

后eg是否自 2024-08-31 01:04:08

您可以通过在 tests.py 中添加/编辑 suite() 函数来完成此操作,该函数定义 django 测试运行程序将运行哪些测试。

import unittest
import doctest
from project import views

def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite(views))
    return suite

然后像往常一样运行你的测试,你应该在views.py 中看到你的文档测试运行。

$ python manage.py test project

django 测试文档对此进行了更详细的描述

当您运行测试时,测试实用程序的默认行为是查找 models.py 和 tests.py 中的所有测试用例(即,unittest.TestCase 的子类),并自动从中构建测试套件测试用例,并运行该套件。

还有第二种方法来定义模块的测试套件:如果您在 models.py 或 tests.py 中定义一个名为 suite() 的函数,Django 测试运行程序将使用该函数来构建测试套件那个模块。这遵循建议的单元测试组织。有关如何构建复杂测试套件的更多详细信息,请参阅 Python 文档。

但是,请记住,构建您自己的测试套件意味着 django 测试运行程序不会自动运行您在tests.py 中的任何测试。例如,您必须手动将它们添加到您的套件中

import unittest
import doctest
from project import views

class FooTestCase(unittest.TestCase):
    def testFoo(self):
        self.assertEquals('foo', 'bar')

def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite(views))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(FooTestCase))
    return suite

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.

import unittest
import doctest
from project import views

def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite(views))
    return suite

Then just run your tests as usual and you should see your doctests in views.py run.

$ python manage.py test project

This is described in more detail in the django testing documentation

When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in models.py and tests.py, automatically build a test suite out of those test cases, and run that suite.

There is a second way to define the test suite for a module: if you define a function called suite() in either models.py or tests.py, the Django test runner will use that function to construct the test suite for that module. This follows the suggested organization for unit tests. See the Python documentation for more details on how to construct a complex test suite.

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

import unittest
import doctest
from project import views

class FooTestCase(unittest.TestCase):
    def testFoo(self):
        self.assertEquals('foo', 'bar')

def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite(views))
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(FooTestCase))
    return suite
若水微香 2024-08-31 01:04:08

Django 1.6 中发生了变化

Doctests 将不再被自动发现。整合
doctests 在您的测试套件中,请遵循 Python 中的 建议
文档

所以我自己要做的就是(在 my_app/tests.py 中):

import unittest
import doctest
from my_app import views

def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite(views))
    return tests

Things have changed in Django 1.6:

Doctests will no longer be automatically discovered. To integrate
doctests in your test suite, follow the recommendations in the Python
documentation
.

So all I had to do myself was (in my_app/tests.py):

import unittest
import doctest
from my_app import views

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