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. C#为什么要装箱和拆箱

    来自论坛4楼的回答非常棒,地址:https://bbs.csdn.net/topics/390624164?page=1 内容如下: 道理很简单,按理说C#被设计成一种完全面向对象的语言.因此,包括数 ...

  2. FPGA学习笔记之按键控制

    参考: [黑金原创教程][FPGA那些事儿-驱动篇I ]实验二:按键模块① - 消抖 源码如下: key_funcmod.v module key_funcmod(clk, rst, key, led ...

  3. Windows——Office使用激活工具激活后仍提示激活

    问题: Office使用激活工具激活后仍提示激活 分析: 造成该问题的原因通常是未删除操作系统预置Office导致的, 解决方案: 调出运行,输入regedit打开注册表编辑器, 依次打开   HKE ...

  4. _MyBatis3-topic06.07.08.09_ 全局配置文件_引入dtd约束(xml提示)/ 引入properties引用/ 配置驼峰命名自动匹配 /typeAliases起别名.批量起别名

    MyBatis3 的全局配置文件 : Setting -官方文档 笔记要点 出错分析 [Intellij idea配置外部DTD文件] 设置步骤: (同Eclipse中的Catalog设置 ) Fil ...

  5. python_面向对象——多继承

    1.多继承 class Shenxian: def fly(self): print('神仙会飞~') class Monkey: def eat_peach(self): print('猴子喜欢吃桃 ...

  6. Jenkins 参数化构建(Extended Choice Parameter)

    1.下载安装 Extended Choice Parameter 插件 2.打开job--->General--->参数化构建过程--->Extended Choice Parame ...

  7. Mysql 安装,及其闪退的问题解决

    1.下载 Mysql 64下载链接(华军软件) http://soft.onlinedown.net/soft/3573.htm 2.安装 2.1 傻瓜式安装 不再赘述,略过. 2.2安装完成打开闪退 ...

  8. SIGAI机器学习第十六集 支持向量机3

    讲授线性分类器,分类间隔,线性可分的支持向量机原问题与对偶问题,线性不可分的支持向量机原问题与对偶问题,核映射与核函数,多分类问题,libsvm的使用,实际应用 大纲: 多分类问题libsvm简介实验 ...

  9. POJ P2279 Mr. Young's Picture Permutations 题解

    每日一题 day14 打卡 Analysis 五维dpf[a1,a2,a3,a4,a5]表示各排从左端起分别占了a1,a2,a3,a4,a5个人时合影方案数量然后我们枚举a1,a2,a3,a4,a5从 ...

  10. b/s利用webuploader实现超大文件分片上传、断点续传

    本人在2010年时使用swfupload为核心进行文件的批量上传的解决方案.见文章:WEB版一次选择多个文件进行批量上传(swfupload)的解决方案. 本人在2013年时使用plupload为核心 ...