欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tornado 自带Testing单元测试模块

程序员文章站 2022-06-14 17:35:09
...
# -*- coding: utf-8 -*-
# _test1_.py
import urllib

import time
import tornado
import base64
from tornado.httpclient import AsyncHTTPClient
from tornado.testing import AsyncTestCase
import pbiisi.msg_ws_pb2 as msgif

user_id = '6ebc445e348b11e7a7ab484d7ea41f89'

def init_head(msg):
    msg.head.unique = 'asdfhaskdfkaf'
    msg.head.ver = 160328
    msg.head.if_dt = int(time.time())
    return msg

#AsyncTestCase 可以看做是对 IOLoop 的一个封装,并以 TestCase 的形式提供使用接口。
class Testfunc1(AsyncTestCase):
    #这步如果不写,会报错传入参数没有ioloop,会导致无法进入runTest里面
    def get_new_ioloop(self):
        return tornado.ioloop.IOLoop.instance()

    
    #在调用的时候会通过testing里面的runTest运行,如果不写此方法,会报错:
    #【ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest】
    @tornado.testing.gen_test
    def runTest(self):
        #global user_id
        # print '=======userlogin======'
        # rqmsg = init_head(msgif.rqUserLogin())
        # rqmsg.user = 'wangwu'
        # rqmsg.pwd = '123333'
        # data = {'uuid': user_id, 'pb2': base64.b64encode(rqmsg.SerializeToString())}
        # client = AsyncHTTPClient(self.io_loop)
        # response = yield client.fetch("http://192.168.51.40:10009/userlog/userlogin",method='POST',body=urllib.urlencode(data))
        # msg = msgif.UserLogin()
        # msg.ParseFromString(base64.b64decode(response.body))
        # user_id = msg.uuid
        # print msg

        print '=======areainfo======'
        rqmsg = init_head(msgif.rqAreaInfo())
        rqmsg.area_id.extend([1])
        rqmsg.get_child=0
        data = { 'pb2': base64.b64encode(rqmsg.SerializeToString())}
        client = AsyncHTTPClient(self.io_loop)

        response = yield client.fetch("http://192.168.51.40:10009/area/areainfo",method='POST',body=urllib.urlencode(data))
        msg = msgif.CommAns()
        msg.ParseFromString(base64.b64decode(response.body))
        print msg




运行方式:python -m tornado.test.runtests   _test1_

运行之后结果显示为:

Tornado 自带Testing单元测试模块
            
    
    博客分类: python pythontornado 

 

 

 

 

 

相关标签: python tornado