在 pytest 中使用不同的数据库

4 python sqlite testing django pytest

我正在使用 pytest-django 插件为我的 django 应用程序 pytest 运行测试。我使用 MySQL 数据库作为 Django 的默认数据库。是否有可能仅指定 pytest 使用不同的数据库进行测试,SQLite?

我正在寻找不同的选项(如果有的话),而不是为 py.test --ds=test_settings 创建特殊的 test_seetings.py。

and*_*lme 5

--ds=test_settings 是要走的路!

如果你不想--ds=...每次调用 py.test 都打字,你可以添加一个 pytest.ini 包含以下内容:

[pytest]
DJANGO_SETTINGS_MODULE = test_settings
Run Code Online (Sandbox Code Playgroud)

附带说明:在运行测试时使用与生产中使用的相同类型的数据库是一种很好的做法。pytest-django 提供了--reuse-db在测试运行之间重复使用 MySQL/Postgres 数据库的选项,在为具有大量模型的项目运行一些测试时节省了大量设置时间。