如何在GAE中使用webtest和多处理进行测试

时间:2014-08-06 16:12:51

标签: python google-app-engine webtest

请告诉我如何解决以下问题。我想在多进程中使用webtest但它失败了。

这意味着我想对db.I使用并行测试,而不是多进程。

可能由于使用了其他进程,它无法调用db。

结果

======================================================================
FAIL: test_answer (testlab.LabTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/mock.py", line 1201, in patched
    return func(*args, **keywargs)
  File "/Users/unko/dropbox/test/testlab.py", line 48, in test_answer
    self.assertEqual(u.param,"bar")
AssertionError: 'foo' != 'bar'

----------------------------------------------------------------------

testlab.py

#!-*- coding: utf-8 -*-

import unittest
import webtest
import webapp2
from google.appengine.ext import testbed,ndb
import json
import time
from google.appengine.api import apiproxy_stub_map 
from google.appengine.api import urlfetch_stub 
from mock import patch, Mock
from google.appengine.ext import db
from lab import Lab
from lab import Unko
import multiprocessing


class LabTestCase(unittest.TestCase):
  def setUp(self):
    app = webapp2.WSGIApplication([
      ('/lab', Lab),
      ('/(.*)', Lab)
    ],debug=True)
    self.testapp = webtest.TestApp(app)
    self.testbed = testbed.Testbed()
    self.testbed.setup_env(app_id='sagifugoh')
    self.testbed.activate()
    self.testbed.init_datastore_v3_stub()
    self.testbed.init_memcache_stub()
    self.testbed.init_channel_stub()
    self.testbed.init_urlfetch_stub()
  def tearDown(self):
    self.testbed.deactivate()
  @patch('google.appengine.api.urlfetch.urlfetch_service_pb.URLFetchResponse')
  def test_answer(self, URLFetchResponse):
    def request(param):
      response = self.testapp.post('/lab',{"key":"key","param":param})
    def async(param):
      p = multiprocessing.Process(target=request,args=[param])
      jobs.append(p)
      p.start()
    jobs = []
    u = Unko.get_or_insert("key")
    u.param = "foo"
    u.put()
    async("bar")
    time.sleep(2)
    self.assertEqual(u.param,"bar")

if __name__ == '__main__':
  unittest.main()

lab.py

#!-*- coding: utf-8 -*-
import webapp2
from google.appengine.ext import db

class Unko(db.Model):
  param = db.StringProperty()

class Lab(webapp2.RequestHandler):
  def post(self):
    key =  self.request.get('key')
    param = self.request.get('param')
    u = Unko.get_or_insert(key)
    u.param = param
    u.put()

0 个答案:

没有答案