Django - 使用大型静态数据表进行测试

发布于 2024-09-02 11:24:08 字数 202 浏览 7 评论 0原文

我正在使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2024-09-09 11:24:08

这是我的解决方案:

class xxxx(TestCase):
    def setUp(self):
        import _mysql
        db=_mysql.connect('xxxx', 'xxxx', 'xxxx', "test_xxxxxxx")
        db.query(open('sql/xxxxxx.sql').read())

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:

class xxxx(TestCase):
    def setUp(self):
        import _mysql
        db=_mysql.connect('xxxx', 'xxxx', 'xxxx', "test_xxxxxxx")
        db.query(open('sql/xxxxxx.sql').read())

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

背叛残局 2024-09-09 11:24:08

您应该查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文