Django在views.py中进行了docte

时间:2010-03-04 15:39:23

标签: python django unit-testing doctest

Django 1.4 documentation on tests声明:

  

对于给定的Django应用程序,测试运行器在两个位置查找doctests:

     
      
  • models.py文件。您可以为单个模型定义模块级doctests和/或doctest。通常的做法是将应用程序级doctests放在模型docstrings中的模块docstring和模型级doctests中。

  •   
  • 应用程序目录中名为tests.py的文件 - 即包含models.py的目录。此文件是您要编写的任何和所有doctest的钩子,不一定与模型相关。

  •   

出于好奇,我想知道为什么Django的testrunner仅限于models.py中的doctests,但更实际上我想知道如何扩展testrunner的doctests包括(例如){运行views.py时的{1}}和其他模块。

我很感激任何意见。

谢谢。

布赖恩

7 个答案:

答案 0 :(得分:23)

您可以通过在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中看到你的doctests。

$ python manage.py test project

django testing documentation

中有更详细的描述
  

当您运行测试时,测试实用程序的默认行为是在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

答案 1 :(得分:15)

事情有changed in Django 1.6

  

将不再自动发现Doctests。集成   测试套件中的doctests,请遵循recommendations in the Python documentation

所以我必须做的就是(在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

答案 2 :(得分:8)

这是我的tests/__init__.py实施,基于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

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

答案 3 :(得分:5)

使用带有插件的nosetests进行django(django-sane-testing或django-nose)并使用--with-doctest flag

答案 4 :(得分:2)

Django的原生测试系统基于unittest包。所以它没有那么强大。

我建议你使用nose向后兼容unittest类固醇。与Django test runner that uses nose一起使用。您可以通过多种方式自定义鼻子,包括使用-m标志将其指向自定义测试位置。

答案 5 :(得分:2)

我发布了一个github gist,可以让你在项目的任何文件或模块中运行测试。 Running doctests from specific modules and files

答案 6 :(得分:1)

您可以尝试编写自己的testrunner并查看是否可以包含其他文件以进行文档测试。

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

相关问题