简体   繁体   English

Django在views.py中进行了docte

[英]Django doctests in views.py

The Django 1.4 documentation on tests states: 有关测试的Django 1.4 文档说明:

For a given Django application, the test runner looks for doctests in two places: 对于给定的Django应用程序,测试运行器在两个位置查找doctests:

  • The models.py file. models.py文件。 You can define module-level doctests and/or a doctest for individual models. 您可以为单个模型定义模块级doctests和/或doctest。 It's common practice to put application-level doctests in the module docstring and model-level doctests in the model docstrings. 通常的做法是将应用程序级doctests放在模型docstring中的模块docstring和模型级doctests中。

  • A file called tests.py in the application directory -- ie, the directory that holds models.py. 应用程序目录中名为tests.py的文件 - 即包含models.py的目录。 This file is a hook for any and all doctests you want to write that aren't necessarily related to models. 此文件是您要编写的任何和所有doctest的钩子,它们不一定与模型相关。

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 . 出于好奇,我想知道为什么Django的testrunner仅限于models.py的doctests,但更实际上我想知道如何扩展testrunner的doctests包括(例如) views.py和其他模块运行manage.py test

I'd be grateful for any input. 我很感激任何意见。

Thank you. 谢谢。

Brian 布赖恩

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. 您可以通过在tests.py中添加/编辑suite()函数来执行此操作,该函数定义了django测试运行器将运行的测试。

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. 然后像往常一样运行测试,你应该在views.py中看到你的doctests运行。

$ python manage.py test project

This is described in more detail in the django testing documentation 这在django测试文档中有更详细的描述

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. 运行测试时,测试实用程序的默认行为是在models.py和tests.py中查找所有测试用例(即unittest.TestCase的子类),从这些测试用例中自动构建测试套件,并运行该套件。

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. 还有第二种方法可以为模块定义测试套件:如果在models.py或tests.py中定义一个名为suite()的函数,Django测试运行器将使用该函数为该模块构建测试套件。 This follows the suggested organization for unit tests. 这遵循建议的单元测试组织。 See the Python documentation for more details on how to construct a complex test suite. 有关如何构建复杂测试套件的更多详细信息,请参阅Python文档。

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. 但是,请记住,构建自己的测试套件意味着django测试运行器不会自动运行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

Things have changed in Django 1.6 : Django 1.6中的情况发生了变化

Doctests will no longer be automatically discovered. 将不再自动发现Doctests。 To integrate doctests in your test suite, follow the recommendations in the Python documentation . 要在测试套件中集成doctests,请遵循Python文档中建议

So all I had to do myself was (in my_app/tests.py): 所以我必须做的就是(在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

This is my tests/__init__.py implementation, based on Jesse Shieh answer : 这是我的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

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. 它允许将单元测试拆分为单独的文件,并允许添加包含doctests的任何模块。 Just add the module names to the appropriate list at the top of this file. 只需将模块名称添加到此文件顶部的相应列表中即可。

使用带有django(django-sane-testing或django-nose)插件的nosetests并使用--with-doctest标志

Django's native testing system is based on unittest package. Django的原生测试系统基于unittest包。 So it is not as powerful as it can be. 所以它没有那么强大。

I recommend you using nose that is backward-compatible unittest on steroids. 我建议你使用unittest向后兼容的类固醇鼻子 Use it along with Django test runner that uses nose . 将它与使用鼻子的Django测试运行器一起使用 You can customize nose in many ways including pointing it to custom test locations using -m flag. 您可以通过多种方式自定义鼻子,包括使用-m标志将其指向自定义测试位置。

I posted a github gist that lets you run test in any file or module in your project. 我发布了一个github gist,它允许您在项目的任何文件或模块中运行测试。 Running doctests from specific modules and files 从特定模块和文件运行doctests

You could try to write your own testrunner and see, if you can include other files to be checked for doc tests. 如果可以包含要检查文档测试的其他文件,您可以尝试编写自己的testrunner并查看。

http://docs.djangoproject.com/en/dev/topics/testing/#defining-a-test-runner http://docs.djangoproject.com/en/dev/topics/testing/#defining-a-test-runner

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM