Django - 使用大型静态数据表进行测试
我正在使用“manage.py test”以及使用“dumpdata”创建的JSON固定装置
我的问题是固定装置中的几个表非常大(例如包含美国所有城市名称的一个表)使得运行测试极其缓慢。
由于程序永远不会修改其中几个表(例如,城市名称永远不需要修改),因此为每次测试运行创建和拆除这些表没有多大意义。
有没有更好的方法使用此类数据测试此代码?
I am using "manage.py test" along with a JSON fixture I created using using 'dumpdata'
My problem is that several of the tables in the fixture are very large (for example one containing the names of all cities in the US) which makes running a test incredibly slow.
Seeing as several of these tables are never modified by the program (eg - the city names will never need to be modified), it doesn't make much sense to create and tear down these tables for every test run.
Is there a better way to be testing this code using this kind of data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的解决方案:
sql 文件是我使用 phpMyAdmin 导出的一系列插入语句。读取 sql 语句比导入 JSON 或 YAML 夹具要快得多。这肯定不是最优雅的解决方案,但它确实有效。
根据在运行 Django 测试之前加载 SQL 转储中的第三个答案,您只需将此 sql 文件放入应用程序目录内的“sql”目录中即可。这对我在执行“manage.pysyncdb”时对生产数据库有用,但由于某种原因,在执行“manage.py test”时,该数据实际上并未导入到测试数据库中,即使行“Installing custom SQL for xxxx.xxxx model' 出现在输出中。所以,我在 setUp() 中编写了自己的代码
This was my solution:
The sql file was a sequence of insert statements that I exported using phpMyAdmin. Reading sql statements is much faster than importing a JSON or YAML fixture. This is surely not the most elegant solution, but it worked.
According to the third answer in Loading SQL dump before running Django tests, you just need to drop this sql file in the 'sql' directory inside the app directory. This worked for me for the production database when doing 'manage.py syncdb', but for some reason this data wasn't actually imported into the test database when doing 'manage.py test', even though the line 'Installing custom SQL for xxxx.xxxx model' appeared in the output. So, I wrote my own code inside setUp()
您应该查看 nose 框架。看起来你在加载测试装置和撕裂时有更多的控制:
“nose支持包、模块、类和测试用例级别的装置,因此可以尽可能不频繁地进行昂贵的初始化。有关更多信息,请参阅 Fixtures。”
此外,看起来鼻子有 django 插件:谷歌
希望它会有所帮助。
You should check out the nose framework. Looks like you have more control on when you load test fixtures and when you tear-up :
"nose supports fixtures at the package, module, class, and test case level, so expensive initialization can be done as infrequently as possible. See Fixtures for more."
Furthermore, looks like there are django plugins for nose : on google
Hope it will help.