Google App Engineを利用したアプリケーションのテストをするために、GAEUnitというモジュールがあることがわかりました。試してみたら簡単に利用できて便利なのでメモしておきます。(追記: 「GAEUnitとDjangoのThe test clientを併用する」というメモを書きました。)
- #mce_temp_url#GAEUnitのサイトからモジュールを取得する。
- 解凍し、「gaeunit.py」(※1)をアプリケーションのルートにコピーする。
- 「app.yaml」の「handlers」に「gaeunit.py」を追加する。(※2)
- ルートディレクトリに「test」ディレクトリを作成する。(※3)
- 「test」ディレクトリに、テスト用のモジュールを置く。
- 「http://
/test」にアクセスする。
※1 gaeunit.pyは解凍したディレクトリの「gaeunit-2.0a_for_django/django_app/gaeunit/gaeunit.py」にあります。
※2 app.yaml (main.pyは元々あるものとします)
--- app.yaml ---
handlers:
- url: /test.*
script: gaeunit.py
- url: .*
script: main.py
※3 testディレクトリが無いとエラーが発生します。
以上でテストすることができます。なお、GAEUnitの確認に使ったクラスは以下の通りです。
例) GAEUnitの確認
--- models.py ---
from google.appengine.ext import db
class Item(db.Model):
name = db.StringProperty()
price = db.IntegerProperty()
--- test/test_models.py ---
import unittest
from models import Item
class ItemTest(unittest.TestCase):
def setUp(self):
item1 = Item(key_name=u'item1',
name=u'Item1',
price=1200)
item2 = Item(key_name=u'item2',
name=u'Item2',
price=980)
item1.put()
item2.put()
def tearDown(self):
item1 = Item.get_by_key_name(u'item1')
item2 = Item.get_by_key_name(u'item2')
item1.delete()
item2.delete()
def test_item1(self):
self.assertTrue(Item.get_by_key_name(u'item1'))
def test_item2(self):
self.assertEqual(None, Item.get_by_key_name(u'item99'))
def test_item_name1(self):
self.assertEqual(u'Item1', Item.get_by_key_name(u'item1').name)
def test_item_price1(self):
self.assertEqual(1200, Item.get_by_key_name(u'item1').price)
def test_item_count1(self):
self.assertEqual(2, Item.all().count())
それと、djangoのバージョンエラーが頻出して邪魔だったので、gaeunit.pyも若干修正しました。
—- gaeunit.py —-
67 import re
+ 68 from google.appengine.dist import use_library
+ 69 use_library(‘django', ‘1.2')
70 import django.utils.simplejson