Still wrong
models.py
from django.db import models
class Item(models.Model):
text=models.TextField(default='')
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
from lists.models import Item
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>'))
def test_home_page_returns_correct_html_chonggou(self):
response=self.client.get('/')
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>'))
self.assertTemplateUsed(response,'home.html')
def test_user_home_template(self):
response=self.client.get('/')
self.assertTemplateUsed(response,'home.html')
def test_can_save_a_POST_request(self):
response=self.client.post('/',data={
'item_text':'a new list item'})
self.assertIn('a new list item',response.content.decode())
self.assertTemplateUsed(response, 'home.html')
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item=Item()
first_item.text="the first list item"
first_item.save()
second_item = Item()
second_item.text = "the second list item"
second_item.save()
saved_items=Item.objects.all()
self.assertEquals(saved_items.count(),2)
first_saved_item=saved_items[0]
second_saved_item=saved_items[1]
self.assertEquals(first_saved_item.text,'the first list item')
self.assertEquals(second_saved_item.text, 'the second list item')
python manage.py makemigrations