Python中DJANGO简单测试实例
程序员文章站
2022-07-08 19:24:25
本文实例讲述了Python中DJANGO简单测试的用法。分享给大家供大家参考。具体如下:
这里以facebook*的测试版为例。
仅仅测试用户登录,主要说明测试的使用...
本文实例讲述了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程序设计有所帮助。
上一篇: php静态变量与方法与phar的使用
下一篇: AngularJS的内置过滤器详解