模拟Django Queryset以测试一个需要查询的功能 [英] Mocking a Django Queryset in order to test a function that takes a queryset
问题描述
有没有这样的事情已经存在?
不是我知道的,但为什么不使用实际的查询?测试框架全部设置为允许您在测试中创建示例数据,并在每次测试中重新创建数据库,因此似乎没有任何理由不使用真实的东西。
I have a utility function in my Django project, it takes a queryset, gets some data from it and returns a result. I'd like to write some tests for this function. Is there anyway to 'mock' a QuerySet? I'd like to create an object that doesn't touch the database, and i can provide it with a list of values to use (i.e. some fake rows) and then it'll act just like a queryset, and will allow someone to do field lookups on it/filter/get/all etc.
Does anything like this exist already?
Not that I know of, but why not use an actual queryset? The test framework is all set up to allow you to create sample data within your test, and the database is re-created on every test, so there doesn't seem to be any reason not to use the real thing.
这篇关于模拟Django Queryset以测试一个需要查询的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!