本文实例讲述了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程序设计有所帮助。

标签:
Python,DJANGO,简单测试

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“Python中DJANGO简单测试实例”
暂无“Python中DJANGO简单测试实例”评论...