Django doctests in views.py [英] Django doctests in views.py

查看:88
本文介绍了Django doctests in views.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.4 测试文档说明: / p>

The Django 1.4 documentation on tests states:


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

For a given Django application, the test runner looks for doctests in two places:


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

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

一个名为 test.py 在应用程序目录中,即保存models.py的目录。这个文件是您想要编写的任何和所有doctests的钩子,它们不一定与模型相关。

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.

出于好奇我想知道为什么Django的testrunner被限制在 models.py 中的doctests,但更实际的我想知道如何在运行 manage.py test views.py 和其他模块$ c>。

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.

我将不胜感激。

谢谢。

Brian

推荐答案

您可以通过添加/编辑suite()函数在tests.py中,它定义了django测试运行程序运行的测试。

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

然后只要像往常一样运行你的测试,你应该看到你的doptests在view.py中运行。

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

$ python manage.py test project

这在 django测试文档中有更详细的描述。

This is described in more detail in the django testing documentation


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

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.

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

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.

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

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

这篇关于Django doctests in views.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆