test について

werkzeug にはテストツールも付いてきます。

from werkzeug import Client, BaseResponse

from app import Application

class TestDatastore(unittest.TestCase):
  """
  23.3 unittest
  http://www.python.jp/doc/release/lib/module-unittest.html
  23.3.35 TestCase Object
  http://www.python.jp/doc/release/lib/testcase-objects.html
  """
  def setUp(self):
    APP_ID = u'twisted-mind'
    AUTH_DOMAIN = u'example.com'
    USER_EMAIL = u'voluntas@example.com'
    os.environ['APPLICATION_ID'] = APP_ID
    os.environ['AUTH_DOMAIN'] = AUTH_DOMAIN
    os.environ['USER_EMAIL'] = USER_EMAIL
    os.environ['SERVER_NAME'] = u'example.com'
    os.environ['SERVER_PORT'] = u'80'
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
    stub = datastore_file_stub.DatastoreFileStub(APP_ID,
        '/dev/null', '/dev/null')
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
    apiproxy_stub_map.apiproxy.RegisterStub(
        'user', user_service_stub.UserServiceStub())
    apiproxy_stub_map.apiproxy.RegisterStub(
        'urlfetch', urlfetch_stub.URLFetchServiceStub())
    apiproxy_stub_map.apiproxy.RegisterStub(
        'mail', mail_stub.MailServiceStub())
    self.application = Application()

  def test_sample(self):
    client = Client(self.application, BaseResponse)
    response = client.get('/')
    self.assertEqual(200, response.status_code)

こんな感じで気軽に client のテストが出来ますよ。