tests.py
'''from django.test import TestCase # Create your tests here. class Smokeclass(TestCase): def test_bad_maths(self): self.assertEquals(1+1,3)'''''
from django.urls import resolve
from django.test import TestCase
from lists.views import home_page
from django.http import HttpRequest
class HomePageTest(TestCase):
def test_root_url_resolve_to_home_page_view(self):
found=resolve('/')
# resolve The function is django Functions used internally , For parsing url,
# And map it to the corresponding view function , When checking the site root path "/",
# Can I find home_page function
self.assertEquals(found.func,home_page)
def test_home_page_returns_correct_html(self):
request=HttpRequest()
# establish httprequest object , When a user requests a web page in the browser
# django What I see is httprequest object
response=home_page(request)
# Pass on the object to home_page View
html=response.content.decode('utf8')
# extract content, The result is the original byte , Send a user immediately
# Browser's 0 and 1, Then call .decode(), Original byte
# Convert it to the... Sent to the user html character string
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>To-Do lists</title>',html)
self.assertTrue(html.endswith('</html>'))
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
return HttpResponse('<html><title>To-Do lists</title></html>')
Something to watch out for :
1)html.startswith and html.endswith Writing words
2) html=response.content.decode('utf8')
The encoding format is utf8
3) The request is HttpRequest, Return is HttpResponse