django架构+单元测试表格

django架构+单元测试表格

1)

assertTemplateUsed 是Django TestCase 类提供的测试方法,用于检查响应是使用哪个模板渲染(注意,这个方法只能测试通过测试客户端获取的响应)

2)

dict.get(key, default=None)

参数

key – 字典中要查找的键。
default – 如果指定键的值不存在时,返回该默认值。

返回值

返回指定键的值,如果键不在字典中返回默认值 None 或者设置的默认值。

3)跨站请求伪造csrf

https://blog.csdn.net/weixin_40482816/article/details/114301717
django架构+单元测试表格
home.html

<html>
<title>To-Do lists</title>
<body><form method="post">
    <input name="item_text" id="id_new_item" placeholder="enter a todo item"/>
    {% csrf_token %}
    <table id="id_item_table">
        <tr><td>{{ new_item_text }}</td></tr>
    </table>
</form></body>
</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>')'''
from django.shortcuts import render
'''def home_page(request):
    if request.method=='POST':
        return HttpResponse(request.POST['item_text'])
    return render(request,'home.html')'''
def home_page(request):
    return render(request, 'home.html',{'new_item_text':request.POST.get('item_text',''),})



urls.py

"""superlists URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin

from lists import views
from  django.conf.urls import  url
urlpatterns = [
   # url(r'^admin/', admin.site.urls),
    url(r'^$',views.home_page,name='home'),

]

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函数是django内部使用的函数,用于解析url,
        # 并且将其映射到相应的视图函数上,检查网站根路径时"/",
        # 是否能找到home_page函数
        self.assertEquals(found.func,home_page)
    def test_home_page_returns_correct_html(self):
        request=HttpRequest()
        # 创建httprequest对象,用户在浏览器中请求网页时
        # django看到的就是httprequest对象

        response=home_page(request)
        # 把对象传给home_page视图

        html=response.content.decode('utf8')
        # 提取content,得到结果是原始的字节,即发个用户
        # 浏览器的0和1,随后调用.decode(),把原始字节
        # 转换成发给用户的html字符串

        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')
        # 提取content,得到结果是原始的字节,即发个用户
        # 浏览器的0和1,随后调用.decode(),把原始字节
        # 转换成发给用户的html字符串

        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')
上一篇:odoo-021 Char 字段搜索已设置有空值结果


下一篇:分布式消息队列RocketMQ(一)安装与启动