Skip to content

Commit

Permalink
add test param
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-me committed Sep 8, 2016
1 parent 3f2a53f commit b56a9b0
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 238 deletions.
439 changes: 238 additions & 201 deletions .idea/workspace.xml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion BLL/Bhttpbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ def get(self, url, params={}):
return self.mh.get(url, params)

def post(self, url, files=None, params=None):
return self.mh.post(url, files, params)
return self.mh.post(url, files, params)

# def check_param(self):
Binary file modified BLL/__pycache__/Bhttpbase.cpython-34.pyc
Binary file not shown.
2 changes: 0 additions & 2 deletions COMMON/BaseGoals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
__author__ = 'Administrator'
class Goals(object):
URL = '' # 接收接口url
PARAMS = {} # 接收接口参数
RESULT = 'Fail' # 接收测试结果
REALLY_RESULT = ""
Expand All @@ -9,4 +8,3 @@ class Goals(object):
SUCCESS_SUM = 0
ERROR_NUM = 0
CASE_TOTAL = 0
HEADER = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0'}
Binary file modified COMMON/__pycache__/BaseGoals.cpython-34.pyc
Binary file not shown.
Binary file added COMMON/__pycache__/http_param.cpython-34.pyc
Binary file not shown.
21 changes: 21 additions & 0 deletions COMMON/http_param.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
__author__ = 'Administrator'
# 参数化请求
def str__post_param(list_param):
result = {}
for i in list_param:
result[i["name"]] = i["value"]
return result

def str_get_param(list_param, param):
result = "?"
result += list_param["name"] + "=" + list_param["value"] + "&"
return str_sub(0, len(result)-1, result) + "&" + for_dict(param)

def for_dict(d):
result = ""
for (k, v) in d.items():
result += str(k) + "=" + str(v)
return result

def str_sub(start, end, str):
return str[start:end]
14 changes: 9 additions & 5 deletions COMMON/operateXML.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
__author__ = 'Administrator'
from xml.etree import ElementTree as ET

def getXML(xml, mhttp):
"""
"""
tree = ET.parse(xml)
root = tree.getroot()
i_base = {}
Expand All @@ -15,15 +12,22 @@ def getXML(xml, mhttp):

interfaceName.append(i_base)
for elem in root.findall("InterfaceList"):
i_app = {}
i_app = {"param":[]}
i_app["id"] = elem.find('id').text
i_app["name"] = elem.find('name').text
i_app["method"] = elem.find('method').text
i_app["url"] = elem.find('url').text
i_app["params"] = elem.find('params').text
i_app["hope"] = elem.find('hope').text
i_app["login"] = elem.find('login').text
i_app["isList"] = elem.find('isList').text
# interfaceName.append(i_app)
for p in elem.findall("params"):
param = {}
param["name"] = p.find("name").text
param["type"] = p.find("name").attrib.get("type")
param["value"] = p.find("value").text
param["must"] = p.find("must").text
i_app["param"].append(param)
interfaceName.append(i_app)
# print(interfaceName)
return interfaceName, mhttp
11 changes: 6 additions & 5 deletions DAL/Dhttpbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ class ConfigHttp():
def __init__(self, mhttpbase):
self.mhttpbase = mhttpbase
def get(self, url, params):
url = "http://" + self.mhttpbase.host + ":" + self.mhttpbase.port + url
url = "http://" + self.mhttpbase.host + ":" + self.mhttpbase.port + "/" + url + params
print(url)
try:
r = requests.get(url, params=params, headers={'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
r = requests.get(url, headers={'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0'})
r.encoding = 'UTF-8'
return r.text
Expand All @@ -17,9 +18,9 @@ def get(self, url, params):
return {}
# 封装HTTP POST请求方法,支持上传图片
def post(self, url, files=None, data=None):
data = eval(data)
url = 'http://' + self.mhttpbase.host + ':' + self.mhttpbase.port + url
# data = eval(data)
url = 'http://' + self.mhttpbase.host + ':' + self.mhttpbase.port + "/" + url
r =requests.post(url, files=files, data=data)
# print(data)
json_response = r.text
print(json_response)
return json_response
Binary file modified DAL/__pycache__/Dhttpbase.cpython-34.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,9 @@ def compare(exJson,factJson,isList=0):
### 2016-9-7
* 三层架构重构整个系统

### 2016-9-8
* 新增接口测试扩展
* 检查参数是否必填
* 检查参数的值类型是否正确


2 changes: 1 addition & 1 deletion email.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
to_addr = ["[email protected]", "[email protected]"]
mail_host = smtp.qq.com
mail_user = [email protected]
mail_pass = tagdcyaqvbkqbjff
mail_pass = 1111
port = 465
headerMsg = 接口测试
attach = 接口测试
5 changes: 3 additions & 2 deletions model/Mhttpbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import configparser
from schematics.models import Model
from schematics.types import StringType, URLType
from schematics.types.compound import MultiType
from schematics.types.compound import MultiType,ListType
# BaseHttp类

class BaseHttp(Model):
Expand All @@ -15,6 +15,7 @@ class BaseHttp(Model):
port = StringType(required=True)
No = StringType(required=True)
file = StringType()
param = StringType()
param = ListType(MultiType())
data = MultiType()


Binary file modified model/__pycache__/Mhttpbase.cpython-34.pyc
Binary file not shown.
Binary file modified report.xlsx
Binary file not shown.
38 changes: 17 additions & 21 deletions runner_m.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
from MODEL import Memail
from BLL import BgetEmail,BsendEmail
from BLL import BexcelReport as excel
from COMMON import operateXML
from COMMON import operateXML as om
from BLL import Bhttpbase
from MODEL import Mhttpbase
from BLL import Bresult, BresultDetail
from MODEL import Mresult, MresultDetail
from COMMON import http_param as hp
import json



mresult = Mresult.result()
mresult.info = []
def get_email():
Expand All @@ -31,7 +33,7 @@ def excel_report(wd, data, worksheet_init, worksheet_detail):
ex.detail(worksheet_detail, data[1])

def get_api():
return operateXML.getXML("d:\\app\\auto_http34_test\\test3.xml", Mhttpbase.BaseHttp())
return om.getXML("d:\\app\\auto_http34_test\\test4.xml", Mhttpbase.BaseHttp())

def configHttp(httpbase):
return Bhttpbase.ConfigHttp(httpbase)
Expand All @@ -58,14 +60,15 @@ def function(self):
response = ""
if self.index == 1:
if gm[self.index]["method"] == "POST":
response = self.config_http.post(url=go.URL, params=go.PARAMS)
response = self.config_http.post(url=gm[self.index]["url"], params=hp.str__post_param(gm[self.index]["param"]))
go.REALLY_RESULT = eval(response)
hope = eval(self.hope)
# temp = testJson.compareJson(hope, go.REALLY_RESULT, gm[self.index]["isList"])
temp = check.compare(hope, go.REALLY_RESULT)
if temp:
go.LOGIN_KY = gm[1]["login"]
go.LOGIN_VALUE = go.REALLY_RESULT["content"][0][go.LOGIN_KY]
go.LOGIN_KEY = gm[self.index]["login"]
print(go.LOGIN_KEY)
go.LOGIN_VALUE = go.REALLY_RESULT["content"][0][go.LOGIN_KEY]
go.RESULT = 'Pass'
go.SUCCESS_SUM += 1
else:
Expand All @@ -75,17 +78,17 @@ def function(self):
if gm[self.index]["login"] != "0":
go.PARAMS[go.LOGIN_KEY] = go.LOGIN_VALUE
if gm[self.index]["method"] == "POST":
response = self.config_http.post(go.URL, go.PARAMS)
go.PARAMS = hp.str__post_param(gm[self.index]["param"])
response = self.config_http.post(gm[self.index]["url"], go.PARAMS)
if gm[self.index]["method"] == "GET":
response = self.config_http.get(go.URL)
go.PARAMS = hp.str_get_param(gm[self.index]["param"][0], go.PARAMS)
response = self.config_http.get(gm[self.index]["url"], go.PARAMS)
go.REALLY_RESULT = eval(str(response))
hope = eval(self.hope)
# temp = testJson.compareJson(hope, go.REALLY_RESULT, gm[self.index]["isList"])
temp = check.compare(hope, go.REALLY_RESULT, gm[self.index]["isList"])
if temp:
go.RESULT = 'Pass'
go.SUCCESS_SUM += 1
# except AssertionError:
else:
go.RESULT = 'Fail'
go.ERROR_NUM += 1
Expand All @@ -95,35 +98,30 @@ def function(self):
def get_test_suite(index):
test_suite = unittest.TestSuite()
hope = gm[index]["hope"] # 预期值
test_suite.addTest(TestInterfaceCase("function", hope,index))
test_suite.addTest(TestInterfaceCase("function", hope, index))
return test_suite

# 运行测试用例函数
def run_case(runner):
case_list = httpbase.No
case_list = eval(case_list) # 把字符串类型的list转换为list
temp_case = ""
if len(case_list) == False: #判断是否执行指定的用例ID
temp_case = gm
for index in range(1, len(temp_case)):
go.URL = gm[index]['url']
go.PARAMS = gm[index]["params"]
test_suite = get_test_suite(index)
runner.run(test_suite)
# 记录运行结果
mresult.info.append(json.loads(json.dumps(resultInfo(MresultDetail.resultInfo(), t_id=gm[index]["id"], t_name=gm[index]["name"], t_url=gm[0]["host"] + gm[index]["url"],
t_param=gm[index]["params"], t_actual=go.REALLY_RESULT, t_hope=gm[index]["hope"], t_result=go.RESULT,
mresult.info.append(json.loads(json.dumps(resultInfo(MresultDetail.resultInfo(), t_id=gm[index]["id"], t_name=gm[index]["name"], t_url=gm[0]["host"] +"/" + gm[index]["url"],
t_param=str(go.PARAMS), t_actual=go.REALLY_RESULT, t_hope=gm[index]["hope"], t_result=go.RESULT,
t_method=gm[index]["method"]).to_primitive())))
else:
for i in case_list:
for j in range(1, len(gm)):
if str(i) == gm[j]['id']:
go.URL = gm[j]['url']
go.PARAMS = gm[j]["params"]
test_suite = get_test_suite(j)
runner.run(test_suite)
mresult.info.append(json.loads(resultInfo(MresultDetail.resultInfo, t_id=gm[j]["id"], t_name=gm[j]["name"], t_url=gm[0]["host"] + gm[0]["url"],
t_param=gm[j]["params"], t_actual=go.REALLY_RESULT, t_hope=gm[j]["hope"], t_result=go.RESULT,
mresult.info.append(json.loads(resultInfo(MresultDetail.resultInfo, t_id=gm[j]["id"], t_name=gm[j]["name"], t_url=gm[0]["host"] +"/"+gm[0]["url"],
t_param=str(go.PARAMS), t_actual=go.REALLY_RESULT, t_hope=gm[j]["hope"], t_result=go.RESULT,
t_method=gm[j]["method"]).to_primitive()))

# 运行测试套件
Expand All @@ -140,8 +138,6 @@ def run_case(runner):
worksheet2 = workbook.add_worksheet("测试详情")
data = json.loads(json.dumps(result(mresult, test_date=str(sum_time) + "毫秒", test_sum=go.CASE_TOTAL, test_failed=go.ERROR_NUM, test_version="v2.2", test_pl="python3",
test_net="本地连接", test_name=gm[0]["title"], test_success=go.SUCCESS_SUM, info=mresult.info)))
print("shikun")
print(data)
excel_report(workbook, data, worksheet, worksheet2)

# 发送email
Expand Down
47 changes: 47 additions & 0 deletions test4.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<title>导购码接口测试</title>
<host>dgm.boweixin.com</host>
<port>80</port>
<No>[]</No>
<InterfaceList>
<params>
<name type="str">account</name>
<value>18576759587</value>
<must>1</must>
</params>
<params>
<name type="str">password</name>
<value>222222</value>
<must>1</must>
</params>
<params>
<name type="str">type</name>
<value>0</value>
<must>1</must>
</params>
<isList>0</isList>
<id>1001</id>
<name>登陆</name>
<method>POST</method>
<url>Login</url>
<hope>{"appStatus":{"errorCode":0,"message":"操作成功"},"content":[{"user_sex":0,"fk_user_city":440300,"user_id":30,"nickname":"18576759587","user_phone":"18576759587","head_picture":"http:\/\/dgm.boweixin.com\/","has_finance":1,"user_state":1}]}</hope>
<login>user_id</login>
<isList>0</isList>
</InterfaceList>
<InterfaceList>
<params>
<name type="int">lookerId</name>
<value>2</value>
<must>1</must>
</params>
<id>1002</id>
<name>个人主页</name>
<method>GET</method>
<url>GetPersonalHomePage</url>
<hope>{"appStatus":{"errorCode":0,"message":"操作成功"},"content":[{"business_name":"坤达点子","notice_img":"\/product\/20160718184134_321.jpg","user_type":1,"user_id":2,"goods":[{"good_price":45211.0,"good_id":12,"good_name":"艾欧","banner_picture1":"\/product\/20160719165135_8977.png"},{"good_price":199.0,"good_id":14,"good_name":"麒麟瓜1","banner_picture1":"\/product\/20160720102028_5352.jpg"},{"good_price":452.0,"good_id":6,"good_name":"实力产品","banner_picture1":"\/product\/20160718165448_2602.png"},{"good_price":99898.0,"good_id":11,"good_name":"越南芒果","banner_picture1":"\/product\/20160720100057_5877.jpg"}],"shop_img":"\/product\/20160718120144_3196.jpg","head_picture":"http:\/\/dgm.boweixin.com\/\/product\/20160718134528_4744.jpg","notice_id":1}]}</hope>
<login>1</login>
<isList>1</isList>

</InterfaceList>
</root>

0 comments on commit b56a9b0

Please sign in to comment.