为什么django SimpleTestCase创建测试数据库 [英] why django SimpleTestCase create test database

查看:743
本文介绍了为什么django SimpleTestCase创建测试数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题和 django docs ,在单元测试中使用SimpleTestCase时,django不应创建测试数据库b
$ b

在我的一个应用程序中被称为搜索,我有一些单元测试继承自SimpleTestCase,这是在test.py里面搜索应用程序:

  class TokenizerTestCase(SimpleTestCase):
def test_one(self):
self.assertItemsEqual(1,1)

当我调用 python manage.py test search.tests.TokenizerTestCase

解决方案

默认情况下 SimpleTestCase 创建一个测试数据库。您可以扩展该类的自己的功能。如果你不想在每个安装设置中创建自己的数据库,那么你自己的测试环境扩展类。



覆盖_pre_setup和_post_teardown方法。有关更多信息,请阅读 TransactionTestCase 的源代码,以了解如何创建测试数据库结构。



代码此处


as explained in this question and in django docs, when using SimpleTestCase in unit testing, django should not create test database (which takes too long).

Inside one of my applications which is called "search, I have some unit test inherited from SimpleTestCase. this is tests.py inside search application:

class TokenizerTestCase(SimpleTestCase):
    def test_one(self):
        self.assertItemsEqual(1, 1)

When I call python manage.py test search.tests.TokenizerTestCase it takes too long to build default database. does anybody know why it is creating database for test?

解决方案

By default SimpleTestCase creates a test database. You can extend the class for your own functionality. If you do not want to create a database of your own in each and every setup setup your own test environment extending the classes.

Override the _pre_setup and _post_teardown methods. For more information read the source code for TransactionTestCase to see how it creates the test database structure.

Read the source code here

这篇关于为什么django SimpleTestCase创建测试数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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