6.django建立一个应用
2019-01-03 本文已影响0人
abrila
1.python manage.py startapp lists
2. image.png
3.
test.py
from django.test import TestCase
from django.test import TestCase
from django.test import TestCase
from lists.views import home_page
from django.urls import resolve
from django.http import HttpRequest
# Create your tests here.
class HomePageTest(TestCase):
def test_root_url_resloves_to_home_page_view(self):
found = resolve('/')
self.assertEqual(found.func,home_page)
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
html = response.content.decode('utf-8')
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>To-Do lists</title>',html)
self.assertTrue(html.endswith('</html>'))
4.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>')
5.运行python manage.py test