Django - 单元测试

作者: BlockGeeks | 来源:发表于2014-08-26 00:48 被阅读1494次

Django - 单元测试

Unit tests VS doctest

  • Unit tests —— unittest.TestCase 或 Django自定义的TestCase的子类的方法,更多
    import unittest

      class MyFuncTestCase(unittest.TestCase):
          def testBasic(self):
              a = ['larry', 'curly', 'moe']
              self.assertEqual(my_func(a, 0), 'larry')
              self.assertEqual(my_func(a, 1), 'curly')
    
  • Doctest —— 内嵌在函数帮助文档中的测试,写法类似Python解释器的语法,更多
    def my_func(a_list, idx):
    """
    >>> a = ['larry', 'curly', 'moe']
    >>> my_func(a, 0)
    'larry'
    >>> my_func(a, 1)
    'curly'
    """
    return a_list[idx]

大多数情况下,应该选择单元测试,只有在进行简单测试时才适合选择Doctest

Testing Django Applications (文档)

对于一个Django应用程序(注1),测试执行进程从两个地方寻找单元测试的代码:

  • models.py文件, 测试执行进程在这个模块中寻找unittest.TestCase的子类
  • Django应用程序目录下的tests.py文件,测试执行进程在这个模块中寻找unittest.TestCase的子类

注1: Django应用程序的路径即注册在配置文件(manage.py)中INSTALLED_APPS属性中的项

Django自带的django.test.TestCase会在每次单元测试中创建临时的测试数据库。

运行单元测试
$ python manage.py test     #运行项目中所有APP的单元测试
$ python manage.py test app1    #运行app1下的单元测试
$ python manage.py test app1.A1TestCase  #运行app1下的A1TestCase用例
$ python manage.py test app1.A1TestCase.mehthod1  #同上了类推

Testing Tools

测试客户端

可以使用测试客户端模拟HTTP请求,与Django视图进行交互

  • 模拟GET和POST请求,获取HTTP响应。
  • 查看重定向链,在每一步重定向检查URL和状态码
  • 检查用于模板渲染的模板上下文环境

Django的测试客户端专注于一下两点:

  1. 确保用于渲染模板的模板上下文是正确的
  2. 专注与服务端的返回值,至于页面渲染等HTML,JS测试应该使用其他工具

django.test.client.Client
client实例有post和get方法

class Client(enforce_csrf_checks=False, **defaults)

get(path, data={}, follow=False, **extra)   # **extra关键字参数可以用于指定HTTP头信息中的字段值

post(path, data={}, content_type=MULTIPART_CONTENT, follow=False, **extra)      # 传递JSON数据,content_type=application/json

head(path, data={}, follow=False, **extra)

options(path, data='', content_type='application/octet-stream', follow=False, **extra)

put(path, data='', content_type='application/octet-stream', follow=False, **extra)

delete(path, data='', content_type='application/octet-stream', follow=False, **extra)

注: 增加HTTP 头信息:

  • AUTHORIZATION: HTTP_AUTHORIZATION
  • USER_AGENT: HTTP_USER_AGENT
Testing response

class Response 有以下属性

  • client
  • content
  • context
  • request
  • status_code
  • templates

相关文章

  • 技术博客汇总

    对微信公众号接口的单元测试 Github与Travis CI测试与部署 Django单元测试 NodeJs单元测试...

  • Django单元测试指定配置文件(PyCharm设置)

    创建指定配置文件编写单元测试 创建单元测试Django tests 编辑Configurationimage.pn...

  • Django - 单元测试

    Django - 单元测试 Unit tests VS doctest Unit tests —— unittes...

  • django单元测试总结

    一、django单元测试框架: 编写测试用例: 引用TestCase基类:django.test继承了python...

  • Django 单元测试

    Django自带单元测试的模块django.test,该模块使用了Python的标准库unitest,基于类的方法...

  • Django单元测试基础知识

    相关资源 Django官方文档unittest文档 简单的例子 关于django的单元测试,需要知道的是 对于每一...

  • Django fixtures

    Fixtures在Django测试中的使用 在对Django项目做单元测试时,经常需要生成或者导入一些初始数据。对...

  • Django单元测试

    本文主要介绍基于Django框架开发的web程序进行单元测试。 因为使用django程序的view函数的参数一般为...

  • django单元测试

    在项目开发过程中,单元测试必不可少,通过测试可以减少bug数,提升代码质量。 Django支持单元测试,在添加应用...

  • Django单元测试

    为什么需要自动化测试 对于大项目,对每个单元进行测试可以快速定位错误,确保项目质 量 在 python 中,利用 ...

网友评论

    本文标题:Django - 单元测试

    本文链接:https://www.haomeiwen.com/subject/ntcytttx.html