This document discusses Django testing tools including:
1. Using "manage.py test" to run tests for applications or the entire site. Tests are defined in <app>/tests.py files.
2. Fixtures allow loading initial data from JSON files to use in tests. Fixtures are specified in the TestCase class.
3. The Client allows making requests like GET and POST to test views and check responses. Client stores session data between requests.
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
$
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’})
…