【Testing in Django】

通过参数可控制Django项目不同级别的测试。

1. 运行sign应用下所有的测试用例:

\\guest\python manage.py test sign

2. 运行sign应用下的tests.py测试文件

\\guest\python manage.py test sign.tests

3. 运行sign应用tests.py测试文件下的ModelTest测试类

\\guest\python manage.py test sign.tests.ModelTest

4. 运行sign应用tests.py测试文件下的ModelTest测试类下面的test_event_models测试方法(用例)

\\guest\python manage.py test sign.tests.ModelTest.test_event_models

Creating test database for alias 'default'...
.-
---------------------------------------------------------------------
Ran 1 test in 0.226s
OK
Destroying test database for alias 'default'...

5. 除此之外,还可以使用-p (或--pattern)参数模糊匹配测试文件

\\guest\python manage.py test -p test*.py

【The test views】

用Django自带的TestCase执行测试用例时,要将视图函数中@login_required标签去掉,因为个函数依赖于登录, 然而, Client()所提供的 get()和 post()方法并没有验
证登录的参数。

【代码】

tests.py

from django.test import TestCase
from sign.models import Event,Guest class ModelTest(TestCase):
def setUp(self):
Event.objects.create(id=10,name='oneplus 3 event',status=True,limit=2000,address='shenzhen',start_time='2017-12-12 02:00:56')
Guest.objects.create(id=10,event_id=10,realname='alen',phone='13711001101',email='alen@mail.com',sign=True) def test_event_models(self):
result = Event.objects.get(name='oneplus 3 event')
self.assertEqual(result.address,'shenzhen')
self.assertTrue(result.status) def test_guest_models(self):
result = Guest.objects.get(phone='13711001101')
self.assertEqual(result.realname,'alen')
self.assertTrue(result.sign) class IndexPageTest(TestCase):
'''测试index登录首页''' def test_index_page_renders_index_template(self):
response = self.client.get('/')
self.assertEqual(response.status_code,200)
self.assertTemplateUsed(response,'index.html') from django.contrib.auth.models import User
from django.test import Client
class LoginActionTest(TestCase):
'''测试登录函数'''
def setUp(self):
User.objects.create_user('wxue', 'admin@mail.com', 'admin123456') #创建登录用户,不过不好用,第三个case总是不过,具体原因不明
self.c = Client() def test_login_action_username_password_null(self):
'''用户名密码为空'''
test_data = {'username':'','password':''}
response = self.c.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':'abc','password':'123'}
response = self.c.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':'wxue','password':'admin123456'}
response = self.c.post('/login_action/',data=test_data)
self.assertEqual(response.status_code,302) from sign.models import Event,Guest
from django.test import Client class EventManageTest(TestCase):
#此 用 例 要 想 运 行 通 过 , 需 要 在 views.py 视 图 文 件 中 将 event_manage() 和 search_name() 函 数 的@login_required 装饰器去掉, 因为这两个函数依赖于登录, 然而, Client()所提供的 get()和 post()方法并没有验证登录的参数。
'''发布会管理'''
def setUp(self):
Event.objects.create(id=10,name='xiaomi5',limit=2000,status=True,address='beijing',start_time='2017-12-12 12:00:01')
self.c = Client() def test_event_manage_success(self):
'''测试发布会:xiaomi5'''
response = self.c.post('/event_manage/')
self.assertEqual(response.status_code,200)
self.assertIn(b'xiaomi5',response.content)
self.assertIn(b'beijing',response.content) def test_event_manage_search_success(self):
'''测试发布会搜索'''
response = self.c.get('/search_name/',{'name':'xiaomi5'})
self.assertEqual(response.status_code,200)
self.assertIn(b'xiaomi5',response.content)
self.assertIn(b'beijing',response.content) class GuestManageTest(TestCase): #views.py 视图文件中将 sign_index_action()函数的@login_required 装饰器去掉, 原因同上
'''嘉宾管理'''
def setUp(self):
Event.objects.create(id=10,name='xiaomi5',limit=2000,address='beijing',status=1,start_time='2017-12-12 12:00:02')
Guest.objects.create(realname='alen',phone=18611001100,email='alen@mail.com',sign=0,event_id=10)
self.c = Client() def test_guest_manage_success(self):
'''测试嘉宾信息:alen'''
response = self.c.post('/guest_manage/')
self.assertEqual(response.status_code,200)
self.assertIn(b'alen',response.content)
self.assertIn(b'18611001100',response.content) def test_guest_manage_search_success(self):
'''测试嘉宾搜索'''
response = self.c.get('/search_phone',{'phone':18611001100})
self.assertEqual(response.status_code,200)
self.assertIn(b'alen',response.content)
self.assertIn(b'18611001100',response.content) class SignIndexActionTest(TestCase):
'''发布会签到''' def setUp(self):
Event.objects.create(id=1,name='xiaomi5',limit=2000,address='beijing',status=1,start_time='2017-12-12 12:12:00')
Event.objects.create(id=2,name='oneplus4',limit=2000,address='shenzhen',status=1,start_time='2017-12-13 12:56:56')
Guest.objects.create(realname='alen',phone=18611001100,email='alen@mail.com',sign=0,event_id=1)
Guest.objects.create(realname='una',phone=18611001101,email='una@mail.com',sign=1,event_id=2)
self.c = Client() def test_sign_index_action_phone_null(self):
'''手机号为kong'''
response = self.c.post('/sign_index_action/1/',{'phone':''})
self.assertEqual(response.status_code,200)
self.assertIn(b'phone error',response.content) def test_sign_index_action_phone_or_event_id_error(self):
'''手机号或发布会id错误'''
response = self.c.post('/sign_index_action/2/',{'phone':'18611001100'})
self.assertEqual(response.status_code,200)
self.assertIn(b'event id or phone error',response.content) def test_sign_index_action_user_sign_has(self):
'''用户已经签到'''
response = self.c.post('/sign_index_action/2/',{'phone':'18611001101'})
self.assertEqual(response.status_code,200)
self.assertIn(b'user has sign in',response.content) def test_sign_index_action_sign_success(self):
'''签到成功'''
response = self.c.post('/sign_index_action/1/',{'phone':'18611001100'})
self.assertEqual(response.status_code,200)
self.assertIn(b'sign in success',response.content)

【Django】【四】测试的更多相关文章

  1. django 单独测试模块

    今天单独测试django的一个views文件,出现错误import的模块没有定义,这个模块是在django项目中自己编写的,解决办法: 1../manage.py shell 通过命令行进去加载,再执 ...

  2. 直接用nose进行django项目测试并输出html报告

    先说需求:1.测试django项目:2.打印测试报告(html格式)有以下几种测试方法:1.django自带的测试模块.在app目录下的tests.py文件中写测试类,类似这样: class MyTe ...

  3. Linux Django项目测试

    步骤 django项目: 依赖包 [root@web01 ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readli ...

  4. python+Django+test 测试数据库生成报错

    前提: 使用Django自带的test进行单元测试. 问题描述: 运行:python manage.py test,报错,出现数据库乱码的现象,报错如下: Creating test database ...

  5. Django(四):model

    一.创建model django.db.models是django自带的创建数据库的ORM. 在models.py中以继承models.Model创建表后,需要在setttngs中确保添加了当前应用, ...

  6. django入门-测试-part5

    尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6515996.html 完全翻译自官方文档 https://docs.djangoproje ...

  7. python django 基本测试 及调试

    #########20181110from django.db import modelsfrom blog.models import Article, Author, TagAuthor.obje ...

  8. Django安装 测试、导入项目以及运行开发服务器

    安装Django  下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py install 增加环境变量: C:\Python27\Scripts 测试djang ...

  9. Django Web 测试

    Django 单元测试 模拟浏览器发起请求,测试 web 功能.只是简单记录一下怎么使用. 环境 Win10 Python2.7 Django 1.8.11 MySQL5.6 项目结构 大致如下 my ...

  10. 负载均衡软件LVS分析四(测试)

    一.启动LVS集群服务LVS负载均衡管理和使用有两种方式,一种是以ipvsadm命令行脚步与ldirectord监控方式,一种是以Piranha工具进行管理和使用.下面分别介绍. 1.利用ipvsad ...

随机推荐

  1. ppt插入声音

    1:点击插入>音频>文件中的音频 2:插入成功后,会出现一个声音的图表 3:对播放格式进行设置,设置循环播放等. 4:双击对声音进行编辑 ,会出现右边的各个组件, 5:点击下拉框>效 ...

  2. hdu1305Immediate Decodability(字典树)

    这题看是否 这题能A是侥幸,解决的办法是先存一下输入的字符串,进行排序. Problem Description An encoding of a set of symbols is said to ...

  3. iOS 开发笔记-Storyboard

    什么时候用Pust,什么时候用Modal? 一般情况下,是导航控制器点过去的,都使用Pust.如果是相对独立的,则用Modal,比如是导航上面的+添加之类. 关闭一个Modal -(IBAction) ...

  4. cocos代码研究(1)Node学习笔记

    理论部分 Node类继承自Ref类,是cocos框架中基础底层的一个封装类,与画面渲染相关的类一般都是继承自该类,例如Scene,Layer,Sprite,Sprite3D,Label,SpriteB ...

  5. 527D Clique Problem 判断一维线段没有两辆相交的最大线段数量

    这题说的是给了n个位置 在x轴上 每个位置有一个权值为wi,然后将|xi - xj|>=wi+wj ,满足这个条件的点建一条边,计算着整张图中有多少多少个点构成的子图,使得这个子图的节点数尽量的 ...

  6. Sa身份登陆SQL SERVER失败的解决方案

    经常使用windows身份登陆,久而久之,基本不动怎么用SQL SERVER身份验证登陆,所以趁着有空,就解决一下一些问题~~ 解决方案:  第一步:打开SSMS,先使用windows身份登陆,右击服 ...

  7. suse zypper 添加源

    一.查看源和仓库 1.查看repos (软件仓库) zypper lr 2.查看services(软件源) zypper ls 二.删除源和仓库 1.删除软件仓库 zypper rr name 2.删 ...

  8. javashop组件开发指南

    javashop组件开发指南 1.      概念解释 组件:可以理解为是插件,功能点的一个集合. 插件:是指具体的某个功能. 插件桩:是负责调用插件. 事件:是要决定什么时候执行插件 一个组件是由多 ...

  9. [转载]LinkButton跳转页面及传递参数

    在DataList中使用LinkButton按钮(LinkButtonDelete),该按钮用于链接跳转到删除页面.在模板中双击该按钮,跳转到.cs页面.问题是我们如何获得该条信息的ID,如果不知道I ...

  10. SQL语句调优汇总

    1.插入数据的表或临时表,预先创建好表结构,能够加快执行速度 2.where 条件判断的字段以及连接查询的条件字段   都添加上索引   能够加快执行速度 3.尽量避免使用 like ,类似 like ...