開発したアプリなど一覧

Testing Django Applications

アフィリエイトリンクを含む場合があります

先日の tutorial で作成したpollアプリケーションのテストを書いてみるテスト
とりあえず Poll クラスだけだけど。

詳しくは

Django | Testing Django applications | Django documentation


を参照に・・・

Django でテストを書く方法は主に2種類。

doctest
unittest

とりあえず2種類両方触ってみた。

まずdoctest
class の最初にコメントアウトされているものが doctest
Python インタプリタのように書かないといけない様だ。

models.py

class Poll(models.Model): """ >>> hoge = Poll.objects.create(question="hoge",pub_date=datetime.date.today()) >>> fuga = Poll.objects.create(question="fuga",pub_date=datetime.date(2010,10,1)) >>> print hoge.question hoge >>> print fuga.question fuga >>> hoge.was_published_today() True >>> fuga.was_published_today() False """

question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published')

def __unicode__(self): return self.question

def was_published_today(self): return self.pub_date == datetime.date.today() was_published_today.short_description = 'Published today?'

次に unittest 。
アプリケーションを作成した際に tests.py というファイルが作成されるので、それにテストを書いていく。

tests.py

import unittest from mysite.polls.models import Poll import datetime

class PollTestCase(unittest.TestCase): def setUp(self): self.hoge = Poll.objects.create(question="hoge", pub_date=datetime.date.today()) self.fuga = Poll.objects.create(question="fuga", pub_date=datetime.date(2010,10,1)) def testSetUp(self): self.assertEqual(self.hoge.question, 'hoge') self.assertEqual(self.fuga.question, 'fuga') def testWasPublishedToday(self): self.assertEqual(self.hoge.was_published_today(), True) self.assertEqual(self.fuga.was_published_today(), False)

この状態でテストを走らせる。

mysite$ python manage.py test Creating test database... Creating table auth_permission Creating table auth_group Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site Creating table polls_poll Creating table polls_choice Creating table django_admin_log Installing index for auth.Permission model Installing index for auth.Message model Installing index for polls.Choice model Installing index for admin.LogEntry model ..................................... --------------------------------------------------------------------- Ran 37 tests in 0.807s

OK Destroying test database...

エラーが無ければOKと表示。

試しにわざとエラーを出してみる。
test.py の
"self.assertEqual(self.fuga.was_published_today(), False)"
を True にしてみる。

mysite$ python manage.py test $ python manage.py test ... Installing index for auth.Permission model Installing index for auth.Message model Installing index for polls.Choice model Installing index for admin.LogEntry model ....................................F ====================================================================== FAIL: testWasPublishedToday (mysite.polls.tests.PollTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\data\project\django\mysite\..\mysite\polls\tests.py", line 38, in testWasPublishedToday self.assertEqual(self.fuga.was_published_today(), True) AssertionError: False != True

---------------------------------------------------------------------- Ran 37 tests in 0.936s

FAILED (failures=1) Destroying test database...

FAILED って言われた。ちゃんと動いている様だ。

Sponsored Link

コメント

タイトルとURLをコピーしました