Django 测试开发5 unittest测试用例
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测试用例的更多相关文章
- django测试开发-1.开始Hello django!
用python开发出一个web页面的时候,需要找一个支持python语言的web框架.django框架有丰富的文档和学习资料,也是非常成熟的web开发框架,本篇写一个简单的“hello django! ...
- Django 测试开发4 Django 模板和分页器
Django结合前端框架Bootstrap来开发web页面.pip install django-bootstrap3 在setting.py添加‘bootstrap3’. 继承模板. 在base页面 ...
- Django 测试开发2
1.get方法和post方法 get方法 post方法 直接把method修改成post,报错如下,Django针对CSRF的保护措施是在生成的每个表单放置一个自动生成的令牌,通过这个令牌判断POS ...
- Django 测试开发1
笔者用的版本的是django==1.8.2,这个版本的学习资料最多,文档最完整.首先创建项目:django-admin startproject 项目名. guest/__init__.py 一个空的 ...
- Django 测试开发3 数据模型models和admin管理工具
参考:https://blog.csdn.net/weixin_44510615/article/details/89425412 1.Django模型字段常用类型: IntegerField : 整 ...
- python3的unittest中使用test suite(测试套件)执行指定测试用例
示例代码 module.py class baidumodule(): def __init__(self,driver,): self.dr = driver #不能在类中再次导入webdriver ...
- 测试开发:Python+Django实现接口测试工具
Python+Django接口自动化 引言: 最近被几个公司实习生整自闭了,没有基础,想学自动化又不知道怎么去学,没有方向没有头绪,说白了其实就是学习过程中没有成就感,所以学不下去.出于各种花里胡哨的 ...
- 测试开发中Django和Flask框架
Python测试开发中Django和Flask框架 为了更好地阐述这个问题,我们把开发一个应用的过程进行类比,往往开发一个应用(web应用.系统应用)跟建造房子的过程一样,需要先打地基,搭好骨架,然后 ...
- 《自动化平台测试开发-Python测试开发实战》新书出版了
首先 第一本书,当初在百度阅读初步写了个电子版,刚一上线不久即收到了数百位读者朋友阅读收藏购买,于是顺利成章就出版了纸质书. <软件自动化测试开发>认真看过的读者应该都知道,介绍的主要是自 ...
随机推荐
- axios使用API
背景:请求失败后,因跨域引起的不能传递statusCode问题,通过设置前后台选项解决,这里先总结一下axios的使用 一.安装与配置: 安装: npm install axios axios使用AP ...
- shell 大型脚本工具开发实战
拆分脚本功能,抽象函数 1.function get_all_group 返回进程组列表字符串 2.function get_all_process 返回进程名列表字符串"nginx htt ...
- Windows 在 git bash下使用 conda 命令
1. 安装git 安装连接:http://git-scm.com/download/linux (LINUX) https://git-scm.com/downloads (Windows) 2. ...
- JVM系列四:类加载
类的生命周期 加载->验证->准备->解析->初始化->使用->卸载 类加载过程 类加载包括以上的前五个过程:加载,验证,准备,解析,初始化 加载 1.主要完成三个 ...
- 集合(python)
# -*- coding: utf-8 -*- class Array(object): def __init__(self, size=32, init=None): self._size = si ...
- [转]神奇的 SQL 之层级 → 为什么 GROUP BY 之后不能直接引用原表中的列
原文:https://www.cnblogs.com/youzhibing/p/11516154.html 这篇文章,对group by的讲解不错 -------------------------- ...
- P2P system: Napster
Napster structure client machines之所以叫peers是因为对于server来说这些machines是平等对待的 当你upload一首歌曲如PennyLane.mp3时, ...
- C#格式化信息,格式化数字、格式化日期
一.格式化方法: 1.ToString()实例方法 使用当前文化: varname.ToString("C4"); 使用特定文化: varname.ToString("C ...
- Kubernetes 从懵圈到熟练 – 集群网络详解(转)
阿里云K8S集群网络目前有两种方案,一种是flannel方案,另外一种是基于calico和弹性网卡eni的terway方案.Terway和flannel类似,不同的地方在于,terway支持Pod弹性 ...
- rsync详细解读
本文通过示例详细分析rsync算法原理和rsync的工作流程,是对rsync官方技术报告和官方推荐文章的解释.本文不会介绍如何使用rsync命令(见rsync基本用法),而是详细解释它如何实现高效的增 ...