This assumes that you have read the documentation about starting a new Django project. Let us assume that the main app in your project is named td (short for test driven). To create your first test, create a file named test_view.py and copy paste the following content into it.
from django.test import Client, TestCase
class ViewTest(TestCase):
def test_hello(self):
c = Client()
resp = c.get('/hello/')
self.assertEqual(resp.status_code, 200)
You can run this test by
./manage.py test
and it will most naturally fail! You will see an error similar to the following.
Traceback (most recent call last):
File "/home/me/workspace/td/tests_view.py", line 9, in test_hello
self.assertEqual(resp.status_code, 200)
AssertionError: 200 != 404
Why does that happen? Because we haven't defined a view for that! So let's do it. Create a file called views.py and place in it the following code
from django.http import HttpResponse
def hello(request):
return HttpResponse('hello')
Next map it to the /hello/ by editing urls py as follows:
from td import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', views.hello),
....
]
Now run the test again ./manage.py test
again and viola!!
Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.004s
OK