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)

最新文章

  1. winform总结3> 有趣的bat/winform程序完成自己的任务,然后把自己删除
  2. Gap Buffer
  3. C#语法知识
  4. Activity类生命周期
  5. zigbee学习之路(九):串口(发送)
  6. Winform使用外部浏览器解决webbrowser问题
  7. 网易实习笔试真题C/C++
  8. noip2008提高组题解
  9. spring aop配置及用例说明(3)
  10. Java日志终极指南
  11. ServletContainerInitializer初始化器
  12. centos vsftpd 553 Could not create file解决方法
  13. Mybatis实现部门表增删改查以及排序
  14. lua table表判断是否为空
  15. ideaJ+maven+javaweb实践: sevlet实现upload&download,javaIO代码
  16. webstorm更改scss输出路径
  17. 【转帖】Mysql多维数据仓库指南 第一篇 第1章
  18. ELKstack5.6.5
  19. SPOJ - NSUBSTR(长度为1-len的字串出现的最大次数
  20. bzoj 2820

热门文章

  1. dnmp安装
  2. Elasticsearch7
  3. Linux下安装php 扩展fileinfo
  4. 使用SpringTask 进行Java定时任务开发
  5. go语言-数据类型及类型之间转换
  6. Vue Router 使用方法
  7. 12-Vue的使用-安装 - 条件渲染
  8. 开发第一个maven示例
  9. BZOJ 4300: 绝世好题 二进制
  10. learning scala stripMargin