zoukankan      html  css  js  c++  java
  • Django Web 测试

    Django 单元测试

    模拟浏览器发起请求,测试 web 功能。只是简单记录一下怎么使用。

    环境

    Win10
    Python2.7
    Django 1.8.11
    MySQL5.6
    

    项目结构

    大致如下

    mysite
      mysite
        settings.py
        urls.py
        views.py
        ...
      templates
        ...
    

    测试

    创建 mysite/mysite/tests.py
    也可以放在 mysite/tests.py (项目目录下)
    也可以放在 myapp/tests.py (应用目录下)

    # -*- coding: utf-8 -*-
    
    from django.test import Client, TestCase
    
    class LittleTestCase(TestCase):
        def setUp(self):
            """
            测试前置任务,创建一个客户端,可以把它看做浏览器。
            """
            self.click = Client()
    
        def test_http_response_status(self):
            """
            请求根目录,验证响应码
            """
            response = self.client.get('/')
            self.assertEqual(response.status_code, 200)
    
        def test_http_response_content(self):
            """
            请求 /get_json/,验证返回 JSON 数据。
            """
            response = self.client.get('/get_json/')
            self.assertEqual(response.content, '{"msg": "success"}')
    
        def test_http_response_context(self):
            """
            请求 /hello/,验证上下文,也就是渲染视图的数据
            """
            data = {
                "name": "jack"
            }
            response = self.client.get('/hello/', data)
            self.assertEqual(response.context["name"], "jack123")
    
    

    执行测试
    Django 找到 test*.py 文件中的 TestCase 子类,进行测试

    python manage.py test
    

    如果 tests.py 放在自己创建的目录中,如 mysite/tests/tests.py,执行测试要指定目录

    python manage.py test tests/
    

    测试结果
    执行了3个测试,全部成功
    需要注意,Django 默认创建一个 test_ 开头的数据库执行测试,测试结束会自动销毁这个数据库。

    Creating test database for alias 'default'...
    ...
    ----------------------------------------------------------------------
    Ran 3 tests in 0.533s
    
    OK
    Destroying test database for alias 'default'...
    

    项目代码

    mysite/mysite/urls.py

    # -*- coding: utf-8 -*-
    
    from django.conf.urls import include, url
    from django.contrib import admin
    from django.views.generic import TemplateView
    from mysite import views
    urlpatterns = [
        url(r'^$', views.index),
        url(r'^get_json/$', views.get_json),
        url(r'^hello/$', views.hello),
    ]
    

    mysite/mysite/views.py

    # -*- coding: utf-8 -*-
    
    import json
    from django.shortcuts import render
    from django.http import HttpResponse
    
    def index(request):
        return render(request, 'mysite/index.html')
    
    def get_json(request):
        result = {
            "msg": "success",
        }
        return HttpResponse(json.dumps(result), content_type="application/json")
    
    def hello(request):
        name = request.GET.get('name')
        data = {
            "name": name + "123"
        }
        return render(request, 'mysite/hello.html', data)
    
    
  • 相关阅读:
    IIS的各种身份验证详细测试
    HTTP Error 401.3 Unauthorized Error While creating IIS 7.0 web site on Windows 7
    C/S and B/S
    WCF ContractFilter mismatch at the EndpointDispatcher exception
    Configure WCF
    Inheritance VS Composition
    Unhandled Error in Silverlight Application, code 2103 when changing the namespace
    Java RMI VS TCP Socket
    Principles Of Object Oriented Design
    Socket处理发送和接收数据包,一个小实例:
  • 原文地址:https://www.cnblogs.com/eoalfj/p/10824038.html
Copyright © 2011-2022 走看看