http协议接口自动化测试3(其他辅助函数)
DBoper.py
#!/usr/bin/env python
# encoding: utf-8
import mysql.connector
import MySQLdb
import MySQLdb.cursors
def DB_operation(sql):
try:
conn = MySQLdb.connect(host = 'ip', user = '账号', passwd = '密码', db = 'horce3', port = 2906, cursorclass = MySQLdb.cursors.DictCursor, use_unicode = True, charset = 'utf8')
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
cur.close()
conn.close()
except MySQLdb.Error,e:
e = None
print 'Mysql Error %d: %s' % (e.args[0], e.args[1])
http.py
#__author__ = 'hrwang'
#coding:utf-8
import urllib
import httplib
import json
import excel
import config
#POST请求,body中是json串的形式
#判断接口地址是否返回200
def test(url):
code=urllib.urlopen(url).getcode()
return code
#get请求,参数在url后
def http_get(interface_url,param):
if interface_url != ''and test(interface_url)==200:
requrl = interface_url+param
headerdata = {"Content-Type":"application/json; charset=utf-8","tf-token":"17dd40ba7cb4a2b7b367c99168768cce","tf-uid":"594956701661"}
conn = httplib.HTTPConnection(config.httpurl)
conn.request(method="POST",url=requrl,body=None,headers = headerdata)
response = conn.getresponse()
if response.status==200:
return response.read()
else:
return "response error"
elif interface_url == '':
return 'interface null'
elif test(interface_url)!=200:
return 'interface error'
config.py
#__author__ = 'hrwang'
#coding:utf-8
httpurl='ip'
readfilepath = r'E:\interface_auto\data\httptest_sun.xls' #excel文件的物理路径
level='BVT'
param=3
sendmail=True
functiontest=True

浙公网安备 33010602011771号