在 Django 中处理测试数据的最佳实践是什么?
我目前每个应用程序使用一个固定装置文件,但随着项目的增长,测试花费的时间太长,我相信为每个测试类加载的(现在很大的)固定装置有问题。
由于担心重复和维护,我避免使用许多较小的固定装置,但我知道这是不可避免的。
不过,在我走这条路之前,我想我会问其他人如何使用固定装置来测试他们的应用程序/项目。
I currently use a single fixtures file per application, but as projects grow, the tests are taking far too long and I believe that the (now large) fixtures being loaded for each test class are at fault.
I've avoided having lots of smaller fixtures because of concerns about duplication and maintenance, but I know think that's unavoidable.
Before I go down that path though, I thought I would ask what others do with fixtures for testing their applications/projects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您遇到了一大堆固定装置的问题。随着测试套件的增长,不断的反序列化/加载确实会增加。我建议编写实用函数来根据需要创建数据,而不是依赖于固定装置。例如,您可能有一个创建新
auth.User
的函数,例如:编写一个函数来生成随机字符串/电子邮件,作为读者的练习:)
Yes you have hit on a problem with a large set of fixtures. The constant deserialization/loading does add up as your test suite grows. I would suggest writing utility functions to create data as you need it rather than relying on fixtures. For instance you might have a function to create a new
auth.User
like:Writing a function to generate a random string/email is left as an exercise for the reader :)
确保使用 sqlite 进行测试。与其他数据库引擎相比,速度有很大差异。
Make sure you use sqlite for testing purposes. There's a considerable difference in speed compared to other db engines.