【问题标题】:How to write unittest for django project that using two database (mysql and mongo)如何为使用两个数据库(mysql和mongo)的django项目编写单元测试
【发布时间】:2021-06-10 06:04:54
【问题描述】:
我的 django 项目使用两种类型的数据库(mariadb 和 mongoengine)我不知道如何编写在同一个测试用例中测试它们的单元测试。请指导我。
现在我尝试使用fixtures_mongoengine 来编写fixture mongo 数据,但不能在同一个测试用例中与mysql fixture 一起使用
【问题讨论】:
标签:
mysql
django
unit-testing
mongoengine
django-fixtures
【解决方案1】:
如果您使用多个数据库,例如:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'other': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'other.sqlite3',
}
}
您可以使用TransactionTestCase 喜欢:
# test.py
class TestMyViews(TransactionTestCase):
databases = {'default', 'other'}
def test_index_page_view(self):
call_some_test_code()
参考文档here。
不知道mongoengine能不能用,你可以试试。