如何为使用两个数据库(mysql和mongo)的django项目编写unittest



我的django项目使用两种类型的数据库(mariadb和mongoengine),我不知道如何编写单元测试,在相同的测试用例中测试它们。请给我指路。

现在我尝试使用fixtures_mongoengine来编写fixture mongo数据,但不能在相同的测试用例中使用mysql fixture

如果您使用多个数据库,如:

# 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()

参考此处的文档

我不知道mongoengine是否有效,你可以试一试。

最新更新