更改 django 中测试的设置(具体为 haystack 搜索引擎)
我有一个通过 django-haystack 使用 SOLR 搜索引擎的项目。搜索引擎位于不同的实时服务器上,并且在测试运行期间触摸它是不可取的(实际上,这是不可能的,因为对该主机的访问受到防火墙的限制)
我正在使用标准 django testrunner。幸运的是,它为我提供了可以根据自己的喜好进行修改的对象测试设置,但事实证明这并不是故事的结局。
django-haystack 中的很多东西都是在导入时实例化的,所以当我在测试运行器中更改测试设置时,已经太晚了,尽管我将 SEARCH_BACKEND 更改为虚拟,测试仍然会调用到SOLR。这个问题并不是 HAYSTACK 特有的 - mongoengine 也会发生同样的问题。任何类级语句(例如 CharField(default=Blah.objects.find(...)))都会在实例化时执行,然后 django 有机会更改设置。
当然,问题的根源在于 Django 设置是一个可怕的全局可变混乱,并且 Django 没有为实例化代码提供集中的位置。鉴于此,对于哪种测试解决方案最简单有什么建议吗?目前我正在考虑一个 shell 脚本,它将 DJANGO_SETTINGS 环境变量更改为 test_settings 并随后运行 ./manage.py 测试。如果我仍然可以通过 ./manage.py 做事,那就更好了。
还有更好的想法吗?有类似问题的人吗?
I have a project that uses a SOLR search engine through django-haystack. The search engine is on the different live server and touching it during the test run is undesirable (actually, it's impossible, since the access to that host is firewalled)
I'm using standard django testrunner. Luckily, it gives me the object test-settings I can modify to my liking, but turns out it's not the end of the story.
A lot of stuff in django-haystack is instantiated at the import-time, so by the time I change test-settings in my test runner it is too late, and despite the fact that I change the SEARCH_BACKEND to dummy, tests still make call to SOLR. The problem is not specific to HAYSTACK - same issue happens with mongoengine. Any class-level statements (eg CharField(default=Blah.objects.find(...))) are executed at the instantiation-time before django has a chance to change settings.
Of course the root of the problem is the fact that Django settings is a scary globally mutable mess and that Django provides no centralized place for the instantiation code. Given that, are there any suggestions on what testing solution will be the easiest? At the moment I'm thinking about a shell script which will change DJANGO_SETTINGS environment variable to test_settings and run ./manage.py test afterwards. It would be nicer if I could still do things via ./manage.py though.
Any better ideas? People with similar problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从此处获取答案并稍加修改。这对我来说非常有用:
那么我的测试看起来像:
I took the answer from here and modified it slightly. This works great for me:
My test, then, looks like: