Django使用加载工具进行单元测试,用于几个相关的应用程序问题 [英] Django unit-testing with loading fixtures for several dependent applications problems

查看:127
本文介绍了Django使用加载工具进行单元测试,用于几个相关的应用程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在为现有的代码进行单元测试。我遇到了下一个问题:

I'm now making unit-tests for already existing code. I faced the next problem:

在运行syncdb创建测试数据库后,Django自动填充几个表,如django_content_type或auth_permissions。

After running syncdb for creating test database, Django automatically fills several tables like django_content_type or auth_permissions.

然后,想象一下,我需要运行一个复杂的测试,就像检查用户注册一样,需要一个数据表和它们之间的连接。

Then, imagine I need to run a complex test, like check the users registration, that will need a lof ot data tables and connections between them.

如果我会尝试使用我的整个现有的数据库来制作固定装置(这对我来说会很方便) - 我会收到错误,如此处。这是因为Django已经填充了像django_content_type这样的表。

If I'll try to use my whole existing database for making fixtures (that would be rather convinient for me) - I will receive the error like here. This happens because, Django has already filled tables like django_content_type.

下一个可能的方法是使用已经填充syncdb表的django dumpdata --exclude选项。但是这也不行,因为如果我从我的db和User Permissions表中使用了由syncdb自动创建的用户和用户组对象,我可以收到错误,因为连接它们的主键现在指向错误。这在 here 中部分更详细地描述了夹具地狱,但是所显示的解决方案看起来不太好)

The next possible way is to use django dumpdata --exclude option for already filled with syncdb tables. But this doesn't work well also, because if I take User and User Group objects from my db and User Permissions table, that was automatically created by syncdb, I can receive errors, because the primary keys, connecting them are now pointing wrong. This is better described here in part 'fixture hell', but the solution shown there doensn't look good)

下一个可能的方案是下一步:

The next possible scheme I see is next:


  1. 我正在运行我的测试; Django创建测试数据库,使syncdb并创建所有这些表。

  2. 在我的测试设置中,我将删除此数据库,创建新的空白数据库。

  3. 从测试设置中的现有数据库加载数据转储


推荐答案

解决了:

syncdb创建了测试数据库后,在setUp部分测试中,我使用 os.system 从我的代码访问shell。然后我只是加载数据库的转储,我想用于测试。

After the syncdb has created the test database, in setUp part of the tests I use os.system to access shell from my code. Then I'm just loading the dump of the database, which I want to use for tests.

所以这样做是这样的:syncdb填充内容类型和一些其他表与数据。然后在setUp中加载sql dump的一部分测试会清除所有以前创建的数据,并获得一个不错的数据库。

So this works like this: syncdb fills contenttype and some other tables with data. Then in setUp part of tests loading the sql dump clears all the previously created data and i get a nice database.

可能不是最好的解决方案,但它的工作原理=

May be not the best solution, but it works=)

这篇关于Django使用加载工具进行单元测试,用于几个相关的应用程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆