概述
本文实例讲述了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简单测试实例的全部内容,希望文章能够帮你解决Python中DJANGO简单测试实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复