【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基础数据类型一

    整数 整数在Python中的关键字用int来表示; 整型在计算机中运于计算和比较 在32位机器上int的范围是:  -2**31-2**31-1,即-2147483648-2147483647 在64 ...

  2. 字符串最长子串匹配-dp矩阵[转载]

    转自:https://blog.csdn.net/zls986992484/article/details/69863710 题目描述:求最长公共子串,sea和eat.它们的最长公共子串为ea,长度为 ...

  3. MVC中的传参并在View中获取

    首先action跳转的模式有如下: redirecttoaction("index");//一个参数时在本controller下,不传入参数. redirecttoaction(a ...

  4. Appium-Python-Client安装

    官网是这个:https://pypi.org/project/Appium-Python-Client/#files 下载下来后是这样的文件不知道怎么安装: 不用下载,直接这样就可以 了~~ 安装后导 ...

  5. ML实践详细经典教程----用例图、顺序图、状态图、类图、包图、协作图

    面向对象的问题的处理的关键是建模问题.建模可以把在复杂世界的许多重要的细节给抽象出.许多建模工具封装了UML(也就是Unified Modeling Language?),这篇课程的目的是展示出UML ...

  6. 两个list对应元素相加

    a=[1,2,3] b=[4,5,6] 现将list a与 list b按位相加,其结果为[5,7,9] 方法一: c=[a[i]+b[i] for i in range(min(len(a),len ...

  7. e.printStackTrace() ; 是什么意思?

    catch(Exception e){e.printStackTrace() ;} 当try语句中出现异常是时,会执行catch中的语句,java运行时系统会自动将catch括号中的Exception ...

  8. ARM_Core的处理器模式与寄存器,结构杂谈

    ARM处理器的工作状态:ARM处理器有两种工作状态.在程序的执行过程中,处理器可以在两种工作状态之间切换,并且不影响 相应寄存器中的内容. ARM状态,此时处理器执行32位对齐的ARM指令:BX指令, ...

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

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

  10. 文件和打印机共享 win7 and xp

    Win7 摘自:https://www.xp510.com/article/4249.html 首先开启服务 方法:开始---所有程序---附件---运行---输入services.msc----确定 ...