📕
innohub
  • KEEP LEARNING
  • WebInfo
    • JS 部分运算符操作
    • javascript 中的object array
    • 错误处理以及异常捕获
    • JavaScript Bases
    • LaoZi & Confucius
  • PyInfo
    • Python3 输入与输出
    • Python3OS
    • python 修饰器的使用
    • python3 与mysql数据库连接使用实例
    • Format-specifier
    • CGI初学踩坑
    • Django 项目测试
    • Assert-info
    • 使用ngnix和uWSGI配置Django
    • write|SVN​
    • Matplotlib 基本使用
    • 重读 Python 官方文档
    • Python3 Base
    • python3 多线程
    • Python3 函数解析
    • python3 str 对象的基本操作
    • protocl buffers
    • Iterator-Generator
    • Django base
    • Decorator 2
    • Export to wheel or egg-info
    • 三. 运算、数据及逻辑
  • GoInfo
    • Info
      • Select 语句使用
      • First class function
      • Work Pools
      • StructTag
      • Go struct
      • 常用函数
      • Strings操作
      • Golang Bases
      • 数组以及切片
      • 文件操作
      • Golang 中的指针类型
      • Golang Map 类型
      • Reflection
      • 函数闭包
      • 接口
      • Panic and Recover
      • Go中的错误处理
      • 并发
      • defer usage
      • Method in golang
      • Object-oriented Programming
      • Goalng 包管理机制
  • RustInfo
    • Info
      • 包、crate、模块
      • Rust 中的错误处理
      • 智能指针
      • 泛型 generics
      • 数据布局与内存对齐
      • Functions and methods
      • 无畏并发
      • Actix-web 基本使用
      • Got from guessing game
      • 结构体的使用
      • Rust 中的函数式语言功能
      • 集合类型
      • 枚举以及模式匹配
      • Slice 类型
      • 生命周期
      • 测试
      • Rust 中的所有权
      • trait
      • 不安全 Rust
      • Format_print
      • Rust 通用编程概念
      • Macro
  • OS
    • info
      • 内存屏障 -- Part 1
      • 内存屏障 -- Part 2
      • CPU 上下文切换
      • 文件读写与零拷贝
      • ELF 文件
  • MySql
    • info
      • MySql 架构与历史
      • 02-key
  • kubernetes
    • 第二章 k8s 基本概念
    • 第一章 Kubernetes -- 伟大舵手
  • Redis
    • info
      • Redis 总览
      • 02-underline-ds
  • Shell&CInfo
    • GCC 与make的使用
    • C++ 中 malloc 与 new
    • 位运算符操作
    • Base of C macro
    • C 语言中 extern 关键字的用法
  • Distributed
    • info
      • 分布式理论概述
  • Java
    • info
      • Java 内存模型
  • Prometheus
    • Prometheus -- 不灭之火
Powered by GitBook
On this page
  • 1. 概述
  • 2. 基本测试技巧
  • Shell test
  • 3. views test
  • 4. 测试越多越好

Was this helpful?

  1. PyInfo

Django 项目测试

[TOC]

1. 概述

测试在项目开发中有着举足轻重的作用,可以提供有效便捷的代码检查.测试可以分为不同的level,有些测试关注于小的细节,比如一个model是否返回所需要的值,有的关注于项目的整体操作,比如输入用户的操作序列是否得到期待的结果.

Django提供了自动测试系统,可以通过编写测试代码提高代码可用性.有的程序猿遵守test-driven devlopment 即测试驱动开发,测试代码先于项目代码实现,,测试驱动开发简单地把所有问题放在了测试用例中,如果在开发过程中的代码实现不能满足测试的需求,系统就会报错,从而简化了寻找系统bug的过程,使用测试的好处如下

  • save your time

    对于较为复杂的应用程序,在不同的组分之间可能有较多的耦合关联,一个元素的改变可能造成应用程序难以预料的行为,检查这些依赖需要大量的时间,但是通过自动测试,可以在瞬间玩策划那个对于各种依赖的检查

  • Tests don't identify problems, they prevent them

    测试并不是开发中的负面因素,很多认为无法意识到的错误可以被自动检测

  • Tests make your code more attractive

  • Tests help teams work together

2. 基本测试技巧

​ 在Django项目中的每一个app下都有一个tests.py文件,该app中所有的行为测试都可以放在该文件中,通过对视图或者行为建立测试函数,运行python3 manage.py test app 即可得到测试结果

import datetime

from django.test import TestCase
from django.utils import timezone

from .models import Question


class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)

    def test_was_published_recently_with_old_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is older than 1 day.
        """
        time = timezone.now() - datetime.timedelta(days=1, seconds=1)
        old_question = Question(pub_date=time)
        self.assertIs(old_question.was_published_recently(), False)

    def test_was_published_recently_with_recent_question(self):
        """
        was_published_recently() returns True for questions whose pub_date
        is within the last day.
        """
        time = timezone.now() - datetime.timedelta(hours=23, minutes=59,                 seconds=59)
        recent_question = Question(pub_date=time)
        self.assertIs(recent_question.was_published_recently(), True)

对于未来问题的测试,要求该函数返回False,可是在最初的实现中并没有得到预期的结果,所以需要进行更改

对于涉及时间点的测试,一般要包含,过去,现在,未来,以及临界点的测试

Shell test

django提供了一个用户模拟端,进行用户行为的模拟,可以在tests.py或者shell中使用

>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()




>>> from django.test import Client
>>> # create an instance of the client for our use
>>> client = Client()





>>> # get a response from '/'
>>> response = client.get('/')
Not Found: /
>>> # we should expect a 404 from that address; if you instead see an
>>> # "Invalid HTTP_HOST header" error and a 400 response, you probably
>>> # omitted the setup_test_environment() call described earlier.
>>> response.status_code
404
>>> # on the other hand we should expect to find something at '/polls/'
>>> # we'll use 'reverse()' rather than a hardcoded URL
>>> from django.urls import reverse
>>> response = client.get(reverse('polls:index'))
>>> response.status_code
200
>>> response.content
b'\n    <ul>\n    \n        <li><a href="/polls/1/">What&#39;s up?</a></li>\n    \n    </ul>\n\n'
>>> response.context['latest_question_list']
<QuerySet [<Question: What's up?>]>

setup_test_environment() 安装了一个template renderer ,提供了访问response.context 的方法,该方法并不会创建一个test database

3. views test

依照之前的实现,所有在未来发布的问题也都会在list展示,所以对views.py进行更改

def get_queryset(self):
    """
    Return the last five published questions (not including those set to be
    published in the future).
    """
    return Question.objects.filter(
        pub_date__lte=timezone.now()
    ).order_by('-pub_date')[:5]

pub_date__lte表示小于等于该筛选值

4. 测试越多越好

可能测试代码会多余项目代码,但是是值得的,虽然会有多余的测试,但是不需要减少,测试代码只需要添加,因为进行自动测试的时间很短,效果却十分显著,一些基本的守则:

  • 对于每一个model view设置一个单独的测试类

  • 每一个想要测试的情况设置一个单独的测试方法\

  • 方法名描述测试行为

PreviousCGI初学踩坑NextAssert-info

Last updated 5 years ago

Was this helpful?