任务队列从视图工作,但从单元测试运行时是UnknownQueueError

mik*_*ana 15 python google-app-engine unit-testing

更新:最初我没有意识到这只在单元测试运行时失败.

我在AppEngine中使用Python有一个工作任务队列.- 手动调用视图时,任务将添加到队列并运行 - 从单元测试调用时,将任务添加到队列失败并显示UnknownQueueError.

在阅读有关遇到问题的其他人时,有一些建议taskqueue_stub可以解决这个问题.但我不确定应该怎么做或为什么.

mik*_*ana 12

编辑:工作答案.我的问题是在单个单元测试中添加存根修复:将其移动到setUp()修复的东西.

在tests.py中

from google.appengine.api import apiproxy_stub_map
import os

class BlahTest(MyAppTestCase)
    def setUp(self):
        '''Ensure dev appserver task queue knows where to find queue.yaml'''
        taskqueue_stub = apiproxy_stub_map.apiproxy.GetStub( 'taskqueue' ) 
        dircontainingqueuedotyaml = os.path.dirname(os.path.dirname( __file__ ))
        taskqueue_stub._root_path = dircontainingqueuedotyaml
Run Code Online (Sandbox Code Playgroud)

这现在有效.

  • 使用Google App Engine Testbed的更简单,更简洁的版本:```self.testbed.init_taskqueue_stub(root_path = dircontainingqueuedotyaml)``` (16认同)
  • 我强烈建议不要使用_root_path,因为这是一个受保护的属性. (5认同)