GAE: unit testing taskqueue with testbed

后端 未结 4 1732
醉梦人生
醉梦人生 2020-12-24 14:36

I\'m using testbed to unit test my google app engine app, and my app uses a taskqueue.

When I submit a task to a taskqueue during a unit test, it appears that the ta

4条回答
  •  [愿得一人]
    2020-12-24 15:07

    You might want to try the following code. Full explanation is here: http://www.geewax.org/task-queue-support-in-app-engines-ext-testbed/

    import unittest
    from google.appengine.api import taskqueue
    from google.appengine.ext import testbed
    
    
    class TaskQueueTestCase(unittest.TestCase):
    
      def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()
        self.testbed.init_taskqueue_stub()
        self.task_queue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)
    
      def tearDown(self):
        self.testbed.deactivate()
    
      def testTaskAdded(self):
        taskqueue.add(url='/path/to/task')
    
        tasks = self.taskqueue_stub.get_filtered_tasks(url='/path/to/task')
        self.assertEqual(1, len(tasks))
        self.assertEqual('/path/to/task', tasks[0].url)
    
    unittest.main()
    

提交回复
热议问题