51Testing软件测试论坛
标题:
python mock 脚本
[打印本页]
作者:
巴黎的灯光下
时间:
2017-6-26 13:17
标题:
python mock 脚本
rt,mock的工具有很多,但是我觉得还是不如自己写脚本mock随意,方便。
#! usr/bin/python #coding=utf-8
import BaseHTTPServer
import urlparse
import time
from SocketServer import ThreadingMixIn
import threading
import os
class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(self):
print 'post message'
parsed_path = urlparse.urlparse(self.path)
paramstr = parsed_path.query
path = parsed_path.path
print paramstr
print path
for param in paramstr.split('&'):
print param
length = self.headers.getheader('content-length');
nbytes = int(length)
data = self.rfile.read(nbytes)
cur_thread = threading.currentThread()
print 'Thread:%s\tdata:%s' % (cur_thread.getName(), data)
message_parts = [ 'just a test']
message = '\r\n'.join(message_parts)
self.send_response(200)
self.end_headers()
self.wfile.write(message)
def do_GET(self):
print 'get message'
parsed_path = urlparse.urlparse(self.path)
paramstr = parsed_path.query
path = parsed_path.path
print paramstr
print path
for param in paramstr.split('&'):
print param
if path == self.getShopLevelURL:
buf = self.getShopLevelResponseBody
else:
buf = 'it works'
self.send_header("Welcome", "Contect")
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.end_headers()
self.wfile.write(buf)
'''获取店铺评级接口'''
getShopLevelURL = '/abtest'
getShopLevelResponseBody = '''
{
"status": 1,
"info": "success",
"data": {
"input": {
"phone_number": "15168329206",
"cookie": null,
"id": "1234567",
"session_id": null,
"active_id": null
},
"abt_type": "b",
"abt_name": "abt_001"
}
}'''
class ThreadingHttpServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass
if __name__ == '__main__':
'''server = BaseHTTPServer.HTTPServer(('0.0.0.0',18360), WebRequestHandler) '''
server = ThreadingHttpServer(('0.0.0.0',18360), WebRequestHandler)
ip, port = server.server_address
'''Start a thread with the server -- that thread will then start one '''
'''more thread for each request'''
server_thread = threading.Thread(target=server.serve_forever)
'''Exit the server thread when the main thread terminates'''
server_thread.setDaemon(True)
server_thread.start()
print "Server loop running in thread:", server_thread.getName()
while True:
pass
复制代码
作者:
八戒你干嘛
时间:
2017-6-26 14:54
太直接了。。。先说明一下遇到什么痛点,为何市面上的 mock 都不合适,然后自己为了解决这个痛点采用什么方案,最后再贴代码。这样大伙比较容易理解。
作者:
悠悠小仙仙
时间:
2017-6-26 14:54
我感觉不错,用什么工具不重要,思路正确就可以,这段代码活很清楚,自己用的惯就好。
作者:
乐哈哈yoyo
时间:
2017-6-26 14:55
我建议改用flask实现,代码量少的很
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2