Django测试用例

Django默认Python的标准库unittest编写测试用例。Django的单元测试类django.test.TestCase 从unittest.TestCase继承而来。在创建Django应用时,默认已经生成了tests.py测试文件。

setUp()初始化方法创建了2条数据,通过下面的测试方法,查询插入的数据,断言数据是否正确。注意:setUp()初始化方法并不会真正向数据库插入数据,所以不用清理测试数据。

千万不能单独运行tests.py文件。Django执行测试文件的命令为:python manage.py test   

from django.test import TestCase

# Create your tests here.
from .models import Event,Guest class ModelsTest(TestCase): def setUp(self):
Event.objects.create(id = 1,name = 'oneplus 3 event',status = True,limit = 2000,
address = 'shenzhen',start_time = '2016-08-31 02:18:22')
Guest.objects.create(id = 1,event_id = 1,realname = 'alen',phone = '',
email = 'alen@mail.com',sign=False) def test_event_models(self):
result = Event.objects.get(id=1)
self.assertEqual(result.address,'shenzhen')
self.assertTrue(result.status,True) def test_guest_models(self):
result = Guest.objects.get(realname = 'alen')
self.assertEqual(result.phone, '')
self.assertFalse(result.sign, False)

运行测试用例:

运行sign应用下的所有用例:python manage.py test sign

运行sign应用下的tests.py测试文件:python manage.py test sign.tests

运行sign应用下的tests.py测试文件下的ModelTest测试类:python manage.py test sign.tests.ModelTest

使用-p参数模糊匹配测试文件:python manage.py test -p test*.py

客户端测试

django.test.client类充当一个虚拟的网络浏览器。可以测试视图views与django的程序来通过脚本交互。

django.test.client可以模拟GET、POST请求,从HTTP到页面内容。可以检查重定向,再检查每一步的URL和status_code,测试一个reuqest被django模板渲染。

class IndexPageTest(TestCase):
'''测试index登录首页''' def test_index_page_renders_index_teplate(self):
'''测试index视图'''
response = self.client.get('/index/') #通过client.get()方法请求/index/路径。
self.assertEqual(response.status_code,200)
# assertTemplateUsed 断言服务器是否给定的是index.html的相应。
self.assertTemplateUsed(response,'index.html')
from django.test import TestCase
from django.contrib.auth.models import User class LoginActionTest(TestCase):
'''测试登录动作'''
def setUp(self):
User.objects.create_user('admintest','admintest@mail.com','admintest123456') def test_add_admintest(self):
'''测试添加用户'''
user = User.objects.get(username = 'admintest')
self.assertEqual(user.username,'admintest')
self.assertEqual(user.email, 'admintest@mail.com') def test_login_action_username_password_null(self):
'''用户名密码为空'''
test_data = {'username':'','password':''}
response = self.client.post('/login_action/',data=test_data)
self.assertEqual(response.status_code,200)
self.assertIn(b'username or password error',response.content) def test_login_action_username_password_error(self):
'''用户名密码错误'''
test_data = {'username':'','password':'abc'}
response = self.client.post('/login_action/',data=test_data)
self.assertEqual(response.status_code,200)
self.assertIn(b'username or password error',response.content) def test_login_action_success(self):
'''登录成功'''
test_data = {'username':'admintest','password':'admintest123456'}
response = self.client.post('/login_action/',data=test_data)
self.assertEqual(response.status_code,302)

上面的代码登录成功会自动跳转到特定页面,所以状态码是302重定向而不是200成功。

下面的代码中测试管理页面和搜索页面必须先登录。

class EventMangeTest(TestCase):
'''发布会管理'''
def setUp(self):
User.objects.create_user('admintest','admintest@mail.com','admintest123456')
Event.objects.create(id=1, name='xiaomiplus', status=True, limit=2000,
address='beijing', start_time='2016-08-31 02:18:22')
self.login_user = {'username':'admintest','password':'admintest123456'} def test_event_manage_success(self):
'''测试发布会:xiaomi i5'''
response = self.client.post('/login_action/',data=self.login_user)
response = self.client.post('/event_manage/')
self.assertEqual(response.status_code,200)
self.assertIn(b'xiaomiplus',response.content)
self.assertIn(b'beijing',response.content) def test_event_manage_search_success(self):
'''测试发布会搜索'''
response = self.client.post('/login_action/',data=self.login_user)
response = self.client.post('/search_name/',{'name':'xiaomiplus'})
self.assertEqual(response.status_code,200)
self.assertIn(b'xiaomiplus',response.content)
self.assertIn(b'beijing',response.content)

嘉宾页面由于需要有发布会和嘉宾的信息,所以不仅要构造和嘉宾的数据而且还需要有发布会的数据。一样要先登录构造登录数据。

class GuestMangeTest(TestCase):
'''嘉宾管理'''
def setUp(self):
User.objects.create_user('admintest','admintest@mail.com','admintest123456')
Event.objects.create(id=1, name='oneplus 3 event', status=True, limit=2000,
address='shenzhen', start_time='2016-08-31 02:18:22')
Guest.objects.create(id=1, event_id=1, realname='alen', phone='',
email='alen@mail.com', sign=False)
self.login_user = {'username':'admintest','password':'admintest123456'} def test_guest_manage_success(self):
'''测试发布会:xiaomi i5'''
response = self.client.post('/login_action/',data=self.login_user)
response = self.client.post('/guest_manage/')
self.assertEqual(response.status_code,200)
self.assertIn(b'alen',response.content)
self.assertIn(b'',response.content) def test_guest_manage_search_success(self):
'''测试发布会搜索'''
response = self.client.post('/login_action/',data=self.login_user)
response = self.client.post('/search_phone/',{'phone':''})
self.assertEqual(response.status_code,200)
self.assertIn(b'alen',response.content)
self.assertIn(b'',response.content)
class SignIndexActionTest(TestCase):
'''发布会签到'''
def setUp(self):
User.objects.create_user('admintest','admintest@mail.com','admintest123456')
Event.objects.create(id=1, name='oneplus 3 event', status=True, limit=2000,
address='shenzhen', start_time='2016-08-31 02:18:22')
Event.objects.create(id=2, name='xiaomi plus', status=True, limit=2000,
address='beijing', start_time='2016-08-31 02:18:22')
Guest.objects.create(id=1, event_id=1, realname='alen', phone='',
email='alen@mail.com', sign=1)
Guest.objects.create(id=2, event_id=2, realname='judywang', phone='',
email='judywang@mail.com', sign=0)
self.login_user = {'username':'admintest','password':'admintest123456'} def test_sign_action_phone_null(self):
'''手机号为空'''
response = self.client.post('/login_action/',data=self.login_user)
response = self.client.post('/sign_index_action/1/',{'phone':''})
self.assertEqual(response.status_code,200)
self.assertIn(b'phone error',response.content) def test_sign_action_phone_or_event_id_error(self):
'''手机号或者发布会id错误'''
response = self.client.post('/login_action/', data=self.login_user)
response = self.client.post('/sign_index_action/2/', {'phone':''})
self.assertEqual(response.status_code, 200)
self.assertIn(b'phone or event_id error', response.content) def test_sign_action_user_sign_has(self):
'''用户已签到'''
response = self.client.post('/login_action/', data=self.login_user)
response = self.client.post('/sign_index_action/1/', {'phone':''})
self.assertEqual(response.status_code, 200)
self.assertIn(b'user has sign in.', response.content) def test_sign_action_sign_success(self):
'''签到成功'''
response = self.client.post('/login_action/', data=self.login_user)
response = self.client.post('/sign_index_action/2/',{'phone':''})
self.assertEqual(response.status_code, 200)
self.assertIn(b'sign in success.', response.content)

Django 测试开发5 unittest测试用例的更多相关文章

  1. django测试开发-1.开始Hello django!

    用python开发出一个web页面的时候,需要找一个支持python语言的web框架.django框架有丰富的文档和学习资料,也是非常成熟的web开发框架,本篇写一个简单的“hello django! ...

  2. Django 测试开发4 Django 模板和分页器

    Django结合前端框架Bootstrap来开发web页面.pip install django-bootstrap3 在setting.py添加‘bootstrap3’. 继承模板. 在base页面 ...

  3. Django 测试开发2

    1.get方法和post方法 get方法  post方法 直接把method修改成post,报错如下,Django针对CSRF的保护措施是在生成的每个表单放置一个自动生成的令牌,通过这个令牌判断POS ...

  4. Django 测试开发1

    笔者用的版本的是django==1.8.2,这个版本的学习资料最多,文档最完整.首先创建项目:django-admin startproject 项目名. guest/__init__.py 一个空的 ...

  5. Django 测试开发3 数据模型models和admin管理工具

    参考:https://blog.csdn.net/weixin_44510615/article/details/89425412 1.Django模型字段常用类型: IntegerField : 整 ...

  6. python3的unittest中使用test suite(测试套件)执行指定测试用例

    示例代码 module.py class baidumodule(): def __init__(self,driver,): self.dr = driver #不能在类中再次导入webdriver ...

  7. 测试开发:Python+Django实现接口测试工具

    Python+Django接口自动化 引言: 最近被几个公司实习生整自闭了,没有基础,想学自动化又不知道怎么去学,没有方向没有头绪,说白了其实就是学习过程中没有成就感,所以学不下去.出于各种花里胡哨的 ...

  8. 测试开发中Django和Flask框架

    Python测试开发中Django和Flask框架 为了更好地阐述这个问题,我们把开发一个应用的过程进行类比,往往开发一个应用(web应用.系统应用)跟建造房子的过程一样,需要先打地基,搭好骨架,然后 ...

  9. 《自动化平台测试开发-Python测试开发实战》新书出版了

    首先 第一本书,当初在百度阅读初步写了个电子版,刚一上线不久即收到了数百位读者朋友阅读收藏购买,于是顺利成章就出版了纸质书. <软件自动化测试开发>认真看过的读者应该都知道,介绍的主要是自 ...

随机推荐

  1. Vue 案例 列表动画实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Access denied for user 'test'@'%' to database 'mysql'

    1.问题描述 今天使用MySQL新建了一个用户,此处假设为test用户,用来作为某安装软件的配置用户(会新建大量的表及视图) mysql> create user 'test'@'%' iden ...

  3. Android笔记(十七) Android中的Service

    定义和用途 Service是Android的四大组件之一,一直在后台运行,没有用户界面.Service组件通常用于为其他组件提供后台服务或者监控其他组件的运行状态,例如播放音乐.记录地理位置,监听用户 ...

  4. java - day012 - 异常 , throws, throw , IO ,RandomAccessFile

    异常 封装错误信息的对象 错误信息 类型        例如: NullPointerExce 空指针 提示消息  出错的行号 异常的继承结构 Throwable | - Error 系统级错误 | ...

  5. Qt中C++与QML交互

    ###main.c部分int main(int argc, char *argv[]){    QString info1 = "xxxxxxxxxxx";    QString ...

  6. [Jenkins][centos]1 持续集成 之 配置VNC,部署Jenkins

    痛点:上一篇的AWS部署的VNC不知为啥挂了,死活连不上,因此改申请京东云做部署Jenkins 预计阅读时间:20分钟 更新软件,安装桌面 yum -y update yum -y groupinst ...

  7. Mybatis3.1-[tp_36-37]-_映射文件_select_resultMap关联查询__分步查询传递多列值&fetchType_discriminator鉴别器

    _分步查询传递多列值&fetchType_discriminator鉴别器 笔记要点出错分析与总结 Department.java bean public class Department { ...

  8. 使用Scrapy框架爬取腾讯新闻

    昨晚没事写的爬取腾讯新闻代码,在此贴出,可以参考完善. # -*- coding: utf-8 -*- import json from scrapy import Spider from scrap ...

  9. springboot项目对接支付宝支付

    支付宝对接文档 一.准备工作 1. 首先要到 蚂蚁金服开发者中心 https://openhome.alipay.com/platform/home.htm 注册商家账户,并认证. 2.下载java版 ...

  10. C# 通过 参数返回 C++ 指针

    参数返回 C++ 指针 C++ 代码 Extern_C BASECORELIBRARY_API char * GetFileByteArray(wchar_t * BinfilePath, wchar ...