【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. Python 列表 extend() 方法

    描述 Python 列表 extend() 方法通过在列表末尾追加可迭代对象中的元素来扩展列表. 语法 extend() 方法语法: L.extend(iterable) 参数 iterable -- ...

  2. git merge和git rebase

    转载于http://blog.csdn.net/wh_19910525/article/details/7554489 git merge是用来合并两个分支的. git merge b # 将b分支合 ...

  3. python对象反射和函数反射

    python的对象反射功能,经常在编程时使用.相比较其它的编程语言使用非常方便.反射就是用字符串来操作对象或者类,模块中的成员. 一.对象的反射 反射功能的实现,由这4个内置函数来实现(hasattr ...

  4. PAT 1034 Head of a Gang[难][dfs]

    1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...

  5. 机器学习 python库 介绍

    开源机器学习库介绍 MLlib in Apache Spark:Spark下的分布式机器学习库.官网 scikit-learn:基于SciPy的机器学习模块.官网 LibRec:一个专注于推荐算法的j ...

  6. photoshop打造超酷炫火焰人像效果

    效果图看上去非常的酷.制作方法跟火焰字过程差不多.唯一不同的是前期的处理,需要用滤镜把人物轮廓路径找出来,去色后再用制作火焰的过程制作.最后把最好的火焰叠加到人物上面,适当用蒙版控制区域即可.原图 最 ...

  7. linux printf

    [root@LocalWeb01 ~]# printf '%s%s%s\n' 1 2 3 41234 [root@LocalWeb01 ~]# printf '%s%s%s' 1 2 3 41234 ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON SetComprise1

    zw版[转发·台湾nvp系列Delphi例程]HALCON SetComprise1 procedure TForm1.Button1Click(Sender: TObject);var image0 ...

  9. JSON自动生成相关类

    开源项目地址:https://jsonclassgenerator.codeplex.com/SourceControl/latest 太好用了,这个

  10. MySQL root账户密码设为“root”后执行命令提示ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

    修改root账户密码为“root”后,提示ERROR 1820 (HY000): You must reset your password using ALTER USER statement bef ...