python - 如何设置 django 在单元测试期间不使用内存数据库?

标签 python django unit-testing sqlite

django 在内存中建立一个数据库用于测试选择的数据库引擎是否是sqlite3。但是,我需要将数据库放在文件系统上。如何更改设置以实现此目的?

最佳答案

根据documentation :

By default the test databases get their names by prepending test_ to the value of the NAME settings for the databases defined in DATABASES. When using the SQLite database engine the tests will by default use an in-memory database (i.e., the database will be created in memory, bypassing the filesystem entirely!). If you want to use a different database name, specify NAME in the TEST dictionary for any given database in DATABASES.

TEST 中指定 NAME 键词典:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        ...
        'TEST': {
            'NAME': '/path/to/the/db/file'
        }
    }
}

请注意,对于 Django 1.6 及以下版本,您应该改为设置 TEST_NAME:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        ...
        'TEST_NAME': '/path/to/the/db/file'
    }
}

关于python - 如何设置 django 在单元测试期间不使用内存数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044289/

相关文章:

python - 将元组元素转换为字典

python - 在散点图中突出显示特定点(基于条件)

python - 使用 PIL(Python 图像库)检测屏幕上的图像

python - 使用 tempfile 写入一个 csv 临时文件

c# - DynamicData - 无法将静态方法移动到另一个类(甚至基类)

python - 如何在不实例化的情况下进行赋值

django - onetoone 字段的反向 select_lated 不起作用

python - 获取 'str' 到 Django forms.FileField()

javascript - Fetch 没有给我回复

ios - 为什么我可以在我的案例中访问单元测试类中的私有(private)方法