Django	
                                	


                                      	
  
             @torufurukawa	
  (#bucho)
Django	
                  	

•  manage.py	
  test	
  
•  Fixtures	
  
•  Clinet
manage.py	
  test	
$	
  python	
  manage.py	
  test	
  -­‐-­‐help	
  
Usage:	
  manage.py	
  test	
  [options]	
  [appname	
  ...]	
  


Runs	
  the	
  test	
  suite	
  for	
  the	
  specified	
  
applications,	
  or	
  the	
  entire	
  site	
  if	
  no	
  
apps	
  are	
  specified.	
  
<app>/tests.py	
                                                 	
from	
  django.test	
  import	
  TestCase	
  
from	
  myapp	
  import	
  extract	
  

class	
  ExtractTest(TestCase):	
  
	
  	
  def	
  test	
  (self):	
  
	
  	
  	
  	
  self.assertEqual(	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  extract(u'       ',u'   '),	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  u'      ')	
  
manage.py	
  test	
                                                                            	
$	
  python	
  manage.py	
  test	
  
Creating	
  test	
  database	
  'default’…	
  
           	
  
...........	
  
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  
Ran	
  17	
  tests	
  in	
  0.419s	
  

OK	
  
$	
  
manage.py	
  test	
  myapp.MyTest	
   	
  
                                  	

$	
  python	
  manage.py	
  test	
  bucho	
  

                                                       	

$	
  python	
  manage.py	
  test	
  bucho.TestShow	

                                                	
  
$	
  cat	
  testdata.json	
  	
  
[	
  
	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  "pk":	
  1,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  "model":	
  "api.systemstatus",	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  "fields":	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "corner":	
  "2",	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "mode":	
  "1",	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "quesQon_id":	
  null	
  
	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  },	
  
                                   	
  
]	
  
manage.py	
  dumpdata	
   	
  
                                      	
$	
  python	
  manage.py	
  dumpdata	
  
[{"pk":	
  1,	
  "model":	
  "api.systemstatus",	
  "fields":	
  
     {"corner":	
  "0",	
  "mode":	
  "0",	
  "quesQon_id":	
  
     76}}	
  …..	
  ]	
  
                                   	
             .                	

$	
  python	
  manage.py	
  dumpdata	
  app	
  
$	
  python	
  manage.py	
  dumpdata	
  app.MyModel	
  
fixtures	
                         	
  
                                                             	
Class	
  MyTest(TestCase):	
  
	
  	
  	
  	
  fixtures	
  =	
  [‘mydata.json’]	
  
                                                      	
	
  	
  	
  	
  def	
  test_xxx(self):	
  
	
  	
  	
  	
  	
  	
  	
  	
  …	
  
SQLite	
  3	
                              	
  
                                                                  	
#	
  se_ngs_test.py	
  
DATABASES	
  =	
  {	
  
	
  	
  	
  	
  'default':	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  'ENGINE':	
  'sqlite3',	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  'NAME':	
  ':memory:',	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  …	
  
	
  	
  	
  	
  }	
  
}	
  
view	
                                                                                 	
  Client	
>>>	
  help(Client)	
  
Help	
  on	
  class	
  Client	
  in	
  module	
  django.test.client:	
  

class	
  Client(__builQn__.object)	
  
	
  |	
  	
  A	
  class	
  that	
  can	
  act	
  as	
  a	
  client	
  for	
  tesQng	
  purposes.	
  
	
  |	
  	
  	
  
	
  |	
  	
  It	
  allows	
  the	
  user	
  to	
  compose	
  GET	
  and	
  POST	
  requests,	
  and	
  
	
  |	
  	
  obtain	
  the	
  response	
  that	
  the	
  server	
  gave	
  to	
  those	
  requests.	
  
	
  …	
  
	
  |	
  	
  Client	
  objects	
  are	
  stateful	
  -­‐	
  they	
  will	
  retain	
  cookie	
  (and	
  
	
  |	
  	
  thus	
  session)	
  details	
  for	
  the	
  lifeQme	
  of	
  the	
  Client	
  instance.	
  
Client.get(	
  )	
                     client.post(	
  )	
class	
  MyTest(TestCase):	
  
	
  	
  def	
  test_get(self):	
  
	
  	
  	
  	
  res	
  =	
  self.client.get(‘/foo’)	
  
	
  	
  	
  	
  self.assertEqual(res.status_code,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  200)	
  
                                                                                         	
	
  	
  def	
  test_post(self):	
  
	
  	
  	
  	
  	
  res	
  =	
  self.client.post(	
  
       	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ‘/bar’,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  {‘key’:’value’})	
  
	
  	
  	
  	
  	
  …
 
                                                       	

Client.session	
                  Django	
                         	
  
                                                                     	
TestCase.assertTemplateUsed	
  
                                        	
  
TestCase.assertFormError	
                      	
  
                                                            	
  
TestCase.assertRedirects	
                                                	
  
Django	
                           	
•  manage.py	
  test	
                 	
  
•  Fixtures	
              	
  
•  Client	
   view	
  

Django

  • 1.
    Django     @torufurukawa  (#bucho)
  • 6.
    Django   •  manage.py  test   •  Fixtures   •  Clinet
  • 7.
    manage.py  test $  python  manage.py  test  -­‐-­‐help   Usage:  manage.py  test  [options]  [appname  ...]   Runs  the  test  suite  for  the  specified   applications,  or  the  entire  site  if  no   apps  are  specified.  
  • 8.
    <app>/tests.py   from  django.test  import  TestCase   from  myapp  import  extract   class  ExtractTest(TestCase):      def  test  (self):          self.assertEqual(                          extract(u' ',u' '),                            u' ')  
  • 9.
    manage.py  test   $  python  manage.py  test   Creating  test  database  'default’…     ...........   -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐   Ran  17  tests  in  0.419s   OK   $  
  • 10.
    manage.py  test  myapp.MyTest     $  python  manage.py  test  bucho   $  python  manage.py  test  bucho.TestShow  
  • 11.
    $  cat  testdata.json     [          {                  "pk":  1,                    "model":  "api.systemstatus",                    "fields":  {                          "corner":  "2",                            "mode":  "1",                            "quesQon_id":  null                  }          },     ]  
  • 12.
    manage.py  dumpdata     $  python  manage.py  dumpdata   [{"pk":  1,  "model":  "api.systemstatus",  "fields":   {"corner":  "0",  "mode":  "0",  "quesQon_id":   76}}  …..  ]   . $  python  manage.py  dumpdata  app   $  python  manage.py  dumpdata  app.MyModel  
  • 13.
    fixtures     Class  MyTest(TestCase):          fixtures  =  [‘mydata.json’]          def  test_xxx(self):                  …  
  • 14.
    SQLite  3     #  se_ngs_test.py   DATABASES  =  {          'default':  {                  'ENGINE':  'sqlite3',                    'NAME':  ':memory:',                    …          }   }  
  • 15.
    view    Client >>>  help(Client)   Help  on  class  Client  in  module  django.test.client:   class  Client(__builQn__.object)    |    A  class  that  can  act  as  a  client  for  tesQng  purposes.    |        |    It  allows  the  user  to  compose  GET  and  POST  requests,  and    |    obtain  the  response  that  the  server  gave  to  those  requests.    …    |    Client  objects  are  stateful  -­‐  they  will  retain  cookie  (and    |    thus  session)  details  for  the  lifeQme  of  the  Client  instance.  
  • 16.
    Client.get(  )   client.post(  ) class  MyTest(TestCase):      def  test_get(self):          res  =  self.client.get(‘/foo’)          self.assertEqual(res.status_code,                                            200)      def  test_post(self):            res  =  self.client.post(                        ‘/bar’,                              {‘key’:’value’})            …
  • 17.
      Client.session Django     TestCase.assertTemplateUsed     TestCase.assertFormError       TestCase.assertRedirects    
  • 18.
    Django   •  manage.py  test     •  Fixtures     •  Client   view