首页 编程教程正文

Python中DJANGO简单测试实例

piaodoo 编程教程 2020-02-02 12:22:03 234 0 python教程

这篇文章主要介绍了Python中DJANGO简单测试,实例分析了DJANGO的用法,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Python中DJANGO简单测试的用法。分享给大家供大家参考。具体如下:

这里以facebook台湾的测试版为例。

仅仅测试用户登录,主要说明测试的使用和django环境的设置。

代码如下:

import os
import sys
import unittest
import hashlib
TEST_MEMBER_ID = 11
SNS_ID = 100002309745702
TEST_SESSION_KEY = '125737724171219|2.AQCp7ctCYXJh4ooW.3600.1310022000.1-100002309745702|qj4000WhhKWGtApOI0icEpzSyZ8' # Need to get from sns platform
SIGNED_REQUEST = 'hoIhyhOT7rLoOaxlJwDoGYIv6-qZizL3q9mbVJkJQJA.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEzMTAwMjIwMDAsImlzc3VlZF9hdCI6MTMxMDAxNzg5MCwib2F1dGhfdG9rZW4iOiIxMjU3Mzc3MjQxNzEyMTl8Mi5BUUNwN2N0Q1lYSmg0b29XLjM2MDAuMTMxMDAyMjAwMC4xLTEwMDAwMjMwOTc0NTcwMnxxajQwMDBXaGhLV0d0QXBPSTBpY0VwelN5WjgiLCJ1c2VyIjp7ImNvdW50cnkiOiJ1cyIsImxvY2FsZSI6ImVuX1VTIiwiYWdlIjp7Im1pbiI6MjF9fSwidXNlcl9pZCI6IjEwMDAwMjMwOTc0NTcwMiJ9'
class LoginTestCase(unittest.TestCase):
  def setUp(self):
    # Set up django environment
    pathname = os.path.dirname(sys.argv[0])
    sys.path.append(os.path.abspath(pathname))
    sys.path.append(os.path.normpath(os.path.join(os.path.abspath(pathname), '../')))
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
    from django.http import HttpRequest
    self.request = HttpRequest()
    from django.contrib.sessions.backends.cache import SessionStore
    self.request.session = SessionStore()
    self.request.session[hashlib.md5(TEST_SESSION_KEY).hexdigest()] = TEST_MEMBER_ID
    self.request.session.save()
    self.request.POST = {'signed_request':SIGNED_REQUEST}
  def tearDown(self):
    pass
  def testloginFromWeb(self):
    # login without session key
    from django.conf import settings
    from platforms.login.login_facebook_tw import login_from_web
    res = login_from_web(self.request)
    self.assertEqual(res.status_code, 200)
    self.assertEqual(res.items(), [('Content-Type', 'text/html; charset=utf-8')])
    self.assertTrue(settings.SNS_URL in res.content)
    self.assertTrue(settings.MEDIA_URL in res.content)
    # Check member
    from apps.members.models import Member
    from apps.members.helper import login_sns_user
    member = login_sns_user(self.request, TEST_SESSION_KEY)
    member_db = Member.objects.get(id=TEST_MEMBER_ID)
    self.assertEqual(member, member_db)
if __name__ == '__main__':
  unittest.main()

其他可以类推。

希望本文所述对大家的Python程序设计有所帮助。

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/1120.html

评论

搜索

菜鸟随机文章