sae python 微信公众项目单元测试框架

广告大笑 关注易生活,关注微信公众:EasyTool

开发的伴侣就是单元测试,没有测试用例的开发,或者说是没有自动测试的开发是噩梦般的。

在sae python微信公众项目的开发过程中,建立单元测试框架可以大大提高开发效性、项目的稳定性。

下面提供我自己使用的简单sae python测试框架(weixin_unitest.py):

# -*- coding: utf-8 -*-
#/usr/bin/env python

import sys, urllib, httplib, time, hashlib, random

# 配置
interface_url = '***.sinaapp.com:80' # 注意不能加http://
interface_path = '/your path'
Token = 'your token'

messages = {
    # 用户关注消息
    'subscribe' : '''<xml><ToUserName><![CDATA[your name]]></ToUserName>
    <FromUserName><![CDATA[tester name]]></FromUserName>
    <CreateTime>123456789</CreateTime>
    <MsgType><![CDATA[event]]></MsgType>
    <Event><![CDATA[subscribe]]></Event>
    <EventKey><![CDATA[EVENTKEY]]></EventKey>
    </xml>''',

    # 用户发送文本信息
    'text': '''<xml><ToUserName><![CDATA[your name]]></ToUserName>
    <FromUserName><![CDATA[test name]]></FromUserName> 
    <CreateTime>1348831860</CreateTime>
    <MsgType><![CDATA[text]]></MsgType>
    <Content><![CDATA[test text]]></Content>
    <MsgId>1234567890123456</MsgId>
    </xml>'''

}

def make_post(action):
    '''模拟用户行为产生的消息提交给接口程序'''

    conn = httplib.HTTPConnection(interface_url)

    headers = { "Content-type": "text/xml",
                "Content-Length": "%d" % len(messages[action])}

    # 生成签名相关变量
    timestamp = int(time.time())

    nonce = random.randint(1,100000)

    signature = makeSignature(Token, timestamp, nonce)

    params = urllib.urlencode({'signature': signature, 'timestamp': timestamp, 'nonce': nonce})

    conn.request("POST", interface_path + "?" +params, "", headers)

    print messages[action]
    conn.send(messages[action])

    response = conn.getresponse()

    print response.status, response.reason

    print response.read()

    conn.close()

def makeSignature(Token, timestamp, nonce):
    '''生成签名'''
    try:
        Token = int(Token)
    except Exception, e:
        pass

    sorted_arr = map(str, sorted([Token, timestamp, nonce]))

    sha1obj = hashlib.sha1()
    sha1obj.update(''.join(sorted_arr))
    hash = sha1obj.hexdigest()

    return hash

def listAction():
    print("======Supported actions:======")
    for i in messages.keys():
        print(i)
    print("==============================")

if __name__ == '__main__':
    if len(sys.argv) < 2:   
        print (u"Please input your action")
        listAction()
    else:
        if (messages.has_key(sys.argv[1])):
            make_post(sys.argv[1])
        else:
            print("No this action")
            listAction()

运行:

python weixin_unitest.py subscribe
python weixin_unitest.py text

结合SAEPython(sae服务器),在本地完成开发测试就很方便了。

广告大笑 关注易生活,关注微信公众:EasyTool

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值