如何配置“manage.py test”与 sqlite 一起运行? (姜戈)

发布于 2024-11-15 06:16:45 字数 139 浏览 6 评论 0原文

我使用 Postgres 进行生产和开发,但我想使用 sqlite 来运行一些测试。我没有看到一种简单的方法来配置一个引擎用于测试,另一个引擎用于开发/生产。我错过了什么吗?是否需要设置环境变量,或者是否有一些不涉及另一个 settings.py 文件的简单方法?

I use Postgres for production and development, but I'd like to use sqlite to run some tests. I don't see an easy way to configure one engine for tests and another for dev / production. Am I missing something? Is there an environment variable to set, or some easy way that doesn't involve another settings.py file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

韵柒 2024-11-22 06:16:45

在您的设置中附加以下行:

import sys
if 'test' in sys.argv or 'test_coverage' in sys.argv: #Covers regular testing and django-coverage
    DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'

确保您的实际数据库设置位于它们之前。

Append the following lines in your settings:

import sys
if 'test' in sys.argv or 'test_coverage' in sys.argv: #Covers regular testing and django-coverage
    DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'

Make sure your actual database setting comes before them.

苏大泽ㄣ 2024-11-22 06:16:45

这不是一个直接的答案,但是,是的,您错过了一个大问题 - 在 SQLite 上测试 Postgres 应用程序很棘手 - 它们如此不同。我建议你创建一个 ram-disk(例如使用 tmpfs)并在那里创建你的 Postgres 测试数据库。它不会像 SQLite 那么快,但可能比存储在 HDD 上的常规 Postgres 数据库快一个数量级。

This is not a direct answer, but yes, you are missing one big problem - testing a Postgres app on SQLite is tricky - they are so different. I suggest you rather create a ram-disk (e.g. using tmpfs) and create your Postgres test database there. It won't be as fast as SQLite, but possibly an order of magnitude faster than regular Postgres database stored on HDD.

装迷糊 2024-11-22 06:16:45

您可以尝试类似于 Zachary Voase 此处建议的设置:
http://blog.zacharyvoase.com/2010/02/03/ django-project-conventions/

(整篇文章很有用,但请向下滚动到“设置”部分,了解此处最相关的部分。)

Zach 的策略是创建设置文件夹并使用 __init__.py 文件将其标记为 python 包。然后,您可以为每种部署类型创建一个单独的子模块,其结构如下:

settings/
|-- __init__.py     # Empty; makes this a Python package
|-- common.py       # All the common settings are defined here
|-- development.py  # Settings for development
|-- production.py   # Settings for production
|-- staging.py      # Settings for staging

遵循此概念,您可以为 postgres 设置一个部署,为 sqlite 设置一个单独的部署,并根据需要分离每个部署的配置。

You could try a setup similar to what is suggested here by Zachary Voase:
http://blog.zacharyvoase.com/2010/02/03/django-project-conventions/

(The entire post is useful, but scroll down to the section on "Settings" for the part most relevant here.)

Zach's strategy is to create a settings folder and marks it as a python package using a __init__.py file. You can then have a separate sub-module for each of your deployment types, structured as follows:

settings/
|-- __init__.py     # Empty; makes this a Python package
|-- common.py       # All the common settings are defined here
|-- development.py  # Settings for development
|-- production.py   # Settings for production
|-- staging.py      # Settings for staging

Following this concept, you could set up a deployment for postgres and a separate deployment for sqlite, and separate the configurations for each as needed.

鹊巢 2024-11-22 06:16:45

我认为按照建议使用 if 'test' in sys.argv 修改 settings.py 是一种黑客行为,例如当您想要在 pytest 中执行多线程测试时,它不起作用。
我认为更好的方法是创建一个单独的 settings_test.py 并添加
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3' 到它。

使用 Django 的 testframework 时,使用 python manage.py test --settings=myapp.settings_test 执行测试。

使用 pytest 时,创建一个 pytest.ini 并插入

[pytest]
 DJANGO_SETTINGS_MODULE = myapp.settings_test

I think modifying the settings.py with if 'test' in sys.argv as suggested is a hack and doesn't work for example when you want multi-threaded test execution in pytest.
I think a better way would be to create a separate settings_test.py and adding
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3' to it.

When using Django's testframework, execute your tests with python manage.py test --settings=myapp.settings_test

When using pytest, create a pytest.ini and insert

[pytest]
 DJANGO_SETTINGS_MODULE = myapp.settings_test
梦幻的味道 2024-11-22 06:16:45

我最终在我的 settings.py 中添加了以下内容。
--keepdb 将在 RAM 中设置 Sqlite DB。

if 'test' in sys.argv:
    for db_test in ['default']: # Add other DBs if needed
        DATABASES[db_test]['ENGINE'] = 'django.db.backends.sqlite3'
        if '--keepdb' in sys.argv:
            DATABASES[db_test]['TEST']['NAME'] = '/dev/shm/' + db_test + '.test.db.sqlite3'

I've end up by adding the following in my settings.py.
The --keepdb will setup the Sqlite DB in RAM.

if 'test' in sys.argv:
    for db_test in ['default']: # Add other DBs if needed
        DATABASES[db_test]['ENGINE'] = 'django.db.backends.sqlite3'
        if '--keepdb' in sys.argv:
            DATABASES[db_test]['TEST']['NAME'] = '/dev/shm/' + db_test + '.test.db.sqlite3'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文