Django 对多个数据库进行单元测试
我正在开发一个 Django 项目,其中我的所有单元测试用例都运行良好。
一旦我引入了第二个数据库,所有从 TestCase 继承的测试用例都被破坏了。在这个阶段,我还没有为第二个数据库构建任何测试用例,但我的路由器工作正常。
当我运行测试时,出现错误
“KeyError:'SUPPORTS_TRANSACTIONS'”
在我看来,它试图检查我设置的所有数据库是否都支持事务,但从未创建第二个数据库。
关于如何使用测试脚本构建第二个数据库的任何想法。
I'm working on a django project where all my unit test cases were working perfectly.
Ass soon as I introduced a second database all my test cases that inherit from TestCase are broken. At this stage I haven't build any test case for that second database but my router is working fine.
When I run the tests I get the error,
"KeyError: 'SUPPORTS_TRANSACTIONS'"
It appears to me that is trying to check that that all the databases that I've got setup support transactions but the second database is never created.
Any ideas on how to have the test script to build the second database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我意识到这是一个相当古老的线程,但我遇到了同样的问题,我的解决方案是将
multi_db = True
标志添加到我的测试用例中,例如:Source https://github.com/django/django/blob/master/django/test/ testcases.py#L861
这会导致 django 在所有数据库上调用
flush
(或者如果它们支持事务则回滚)我也在使用数据库路由器
恐怕我在 Django 中找不到这个文档,所以没有链接
I realise this is quite an old thread, but I ran into it with the same issue, and my resolve was adding the
multi_db = True
flag to my testcase, e.g:Source https://github.com/django/django/blob/master/django/test/testcases.py#L861
This causes django to call
flush
on all databases (or rollback if they support transactions)I too am using a db router
I'm afraid I cant find this in Django's documentation, so no link for that
是的,我有一个类似的问题...我的解决方法是设置 'SUPPORTS_TRANSACTIONS': True 对于设置文件中的每个数据库连接。不确定这是否是修复它的正确方法,但它对我有用。
yes I had a similar problem... my fix was to set 'SUPPORTS_TRANSACTIONS': True for each of the database connections in the settings file. Not sure if this is the correct way to fix it, but it worked for me.
参考该链接
Django 文档多数据库
你可以:
感谢
Referring to that link
Django doc Multi-Db
you can:
thanks to
'SUPPORTS_TRANSACTIONS':True 也对我有用。
然而,我有一种使用数据库路由器的奇怪的多数据库设置。
@user298404:你的多数据库设置是什么样的?
附注对不起;积分不够,无法发表评论...
'SUPPORTS_TRANSACTIONS':True worked for me too.
However I have a kind of weird multiple db setup using database routers.
@user298404: how does your multiple db setup look like?
ps. sorry; not enough points for comment...
这是我目前在生产中使用的多数据库设置:
DB_PRIMARY_MASTER、DB_PRIMARY_SLAVE、DB_MAIL_MASTER 和 DB_MAIL_SLAVE 都是字符串常量,以便它们可以在我的数据库路由器中使用。
提示:DB_PRIMARY_MASTER='default'
我希望这会有所帮助!
Here is a multiple db setup that I currently have in production:
DB_PRIMARY_MASTER, DB_PRIMARY_SLAVE, DB_MAIL_MASTER, and DB_MAIL_SLAVE are all string constants so that they can be used in my database router.
Hint: DB_PRIMARY_MASTER='default'
I hope this helps!