Skip to content

Commit

Permalink
add param check
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-me committed Sep 9, 2016
1 parent b56a9b0 commit f4d1178
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 243 deletions.
307 changes: 142 additions & 165 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions COMMON/BaseGoals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ class Goals(object):
SUCCESS_SUM = 0
ERROR_NUM = 0
CASE_TOTAL = 0

# 定义数据类型
INT = "int"
STR = "str"
FLOAT = "float"
DOUBLE = 3


Binary file modified COMMON/__pycache__/BaseGoals.cpython-34.pyc
Binary file not shown.
Binary file modified COMMON/__pycache__/http_param.cpython-34.pyc
Binary file not shown.
Binary file modified COMMON/__pycache__/operateXML.cpython-34.pyc
Binary file not shown.
52 changes: 36 additions & 16 deletions COMMON/http_param.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
__author__ = 'Administrator'
from COMMON.BaseGoals import Goals as go

# 参数化请求
def str__post_param(list_param):
result = {}
for i in list_param:
result[i["name"]] = i["value"]
return result
class chttp_params():
def __init__(self):
pass
# post参数化
def str__post_param(self, list_param):
result = {}
for i in list_param:
if i["must"] == "1": # 如果参数是必填
result[i["name"]] = self.str_to(i["type"], i["value"])
return result
# get 参数化,param是dict
def str_get_param(self, dict_param, param):
result = "?"
if dict_param["must"] == "1": # 如果参数是必填
result += dict_param["name"] + "=" + dict_param["value"] + "&"
# result += dict_param["name"] + "=" + str(self.str_to(dict_param["type"], dict_param["value"])) + "&"
return self.str_sub(0, len(result)-1, result) + "&" + self.for_dict(param)
# 合并dict
def for_dict(self, d):
result = ""
for (k, v) in d.items():
result += str(k) + "=" + str(v)
return result
# 截取str
def str_sub(self, start, end, str):
return str[start:end]
# 转换类型
def str_to(self, type, s):
elements = {
go.INT: lambda: int(s),
go.STR: lambda: s,
go.FLOAT: lambda: float(s)
}
return elements[type]()

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]
3 changes: 2 additions & 1 deletion COMMON/operateXML.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__author__ = 'Administrator'
from xml.etree import ElementTree as ET

def getXML(xml, mhttp):
tree = ET.parse(xml)
root = tree.getroot()
Expand All @@ -9,7 +10,7 @@ def getXML(xml, mhttp):
i_base["host"] = mhttp.host = root.find("host").text
i_base["port"] = mhttp.port = root.find("port").text
i_base["No"] = mhttp.No = root.find("No").text

mhttp.header = root.find("header").text
interfaceName.append(i_base)
for elem in root.findall("InterfaceList"):
i_app = {"param":[]}
Expand Down
1 change: 1 addition & 0 deletions COMMON/strTo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'Administrator'
32 changes: 19 additions & 13 deletions DAL/Dhttpbase.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
__author__ = 'Administrator'
# -*- coding:utf-8 -*-
import requests
import ast
import json

class ConfigHttp():
def __init__(self, mhttpbase):
self.mhttpbase = mhttpbase
def get(self, url, params):
result = {}
url = "http://" + self.mhttpbase.host + ":" + self.mhttpbase.port + "/" + url + params
print(url)
try:
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
except Exception:
print('no json data returned')
return {}
r = requests.get(url, headers=ast.literal_eval(self.mhttpbase.header))
r.encoding = 'UTF-8'
if r.status_code == 200:
result = json.loads(r.text)
result["status_code"] = r.status_code
print(result)
return result

# 封装HTTP POST请求方法,支持上传图片
def post(self, url, files=None, data=None):
# data = eval(data)
url = 'http://' + self.mhttpbase.host + ':' + self.mhttpbase.port + "/" + url
r =requests.post(url, files=files, data=data)
json_response = r.text
print(json_response)
return json_response
print(url)
r = requests.post(url, files=files, data=data)
result = {}
if r.status_code == 200:
result = json.loads(r.text)
result["status_code"] = r.status_code
print(result)
return result
Binary file modified DAL/__pycache__/Dhttpbase.cpython-34.pyc
Binary file not shown.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,28 @@ def compare(exJson,factJson,isList=0):
* 新增接口测试扩展
* 检查参数是否必填
* 检查参数的值类型是否正确

### 2016-9-9
* 实现检查参数是否必填
* 检查参数的值类型是否正确
* 把http_code的值合并到实际结果

```
<params>
<name type="str">account</name> # type=str表示字符类型
<value>18576759587</value>
<must>1</must> # 1表示必填,0非必填
</params>
<params>
<name type="str">password</name>
<value>222222</value>
<must>1</must>
</params>
<params>
<name type="int">type</name>
<value>0</value>
<must>1</must>
</params>
```


12 changes: 5 additions & 7 deletions model/Mhttpbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

class BaseHttp(Model):
'''配置要测试接口服务器的ip、端口、域名等信息'''
host = StringType(required=True)
title = StringType(required=True)
port = StringType(required=True)
No = StringType(required=True)
file = StringType()
param = ListType(MultiType())
data = MultiType()
host = StringType()
port = StringType()
No = StringType()
header = StringType()



Binary file modified model/__pycache__/Mhttpbase.cpython-34.pyc
Binary file not shown.
Binary file modified report.xlsx
Binary file not shown.
85 changes: 48 additions & 37 deletions runner_m.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from MODEL import Mhttpbase
from BLL import Bresult, BresultDetail
from MODEL import Mresult, MresultDetail
from COMMON import http_param as hp
from COMMON import http_param
import json

import ast


mresult = Mresult.result()
Expand All @@ -27,27 +27,45 @@ def get_email():
email = BgetEmail.read_email(g_email)
return email

# 所有的逻辑参数处理
def str_param():
return http_param.chttp_params()

# 测试报告的输出
def excel_report(wd, data, worksheet_init, worksheet_detail):
ex = excel.ExcelReport(wd, data)
ex.init(worksheet_init, data[0])
ex.detail(worksheet_detail, data[1])

# 得到xml配置接口信息和http请求实体类
def get_api():
return om.getXML("d:\\app\\auto_http34_test\\test4.xml", Mhttpbase.BaseHttp())

# 设置http请求实体类
def configHttp(httpbase):
return Bhttpbase.ConfigHttp(httpbase)

gm = get_api()[0] #读取api_xml
httpbase = get_api()[1] #读取http参数

def resultInfo(mresultinfo, **kwargs):

# 接口详情页的excel
def resultInfo(mresultinfo, **kwargs):
return BresultDetail.resultInfo(mresultinfo, **kwargs)

# 接口初始页的excel
def result(mresult, **kwargs):
return Bresult.result(mresult, **kwargs)

# 错误日志统计
def sum_test_data(flag):
if flag:
go.RESULT = 'Pass'
go.SUCCESS_SUM += 1
else:
go.RESULT = 'Fail'
go.ERROR_NUM += 1

# 测试用例(组)类
class TestInterfaceCase(unittest.TestCase):
def __init__(self, testName, hope, index):
Expand All @@ -57,41 +75,33 @@ def __init__(self, testName, hope, index):
def setUp(self):
self.config_http = configHttp(httpbase)
def function(self):
response = ""
if self.index == 1:
temp_result = False
hp = str_param()
if self.index == 1: #第一个xml预留的主要是登陆
if gm[self.index]["method"] == "POST":
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_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
go.REALLY_RESULT = self.config_http.post(url=gm[self.index]["url"], params=hp.str__post_param(gm[self.index]["param"]))
if go.REALLY_RESULT.get("status_code") == 200:
hope = ast.literal_eval(self.hope)
temp_result = check.compare(hope, go.REALLY_RESULT)
if temp_result:
go.LOGIN_KEY = gm[self.index]["login"]
print(go.LOGIN_KEY)
go.LOGIN_VALUE = go.REALLY_RESULT["content"][0][go.LOGIN_KEY]
else:
go.RESULT = 'Fail'
go.ERROR_NUM += 1
pass
else:
if gm[self.index]["login"] != "0":
go.PARAMS[go.LOGIN_KEY] = go.LOGIN_VALUE
go.PARAMS[go.LOGIN_KEY] = go.LOGIN_VALUE # 登陆成功后返回过来的值,可能是token,userid等
if gm[self.index]["method"] == "POST":
go.PARAMS = hp.str__post_param(gm[self.index]["param"])
response = self.config_http.post(gm[self.index]["url"], go.PARAMS)
go.PARAMS = hp.str__post_param(gm[self.index]["param"])
go.REALLY_RESULT = self.config_http.post(gm[self.index]["url"], go.PARAMS)
if gm[self.index]["method"] == "GET":
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 = check.compare(hope, go.REALLY_RESULT, gm[self.index]["isList"])
if temp:
go.RESULT = 'Pass'
go.SUCCESS_SUM += 1
else:
go.RESULT = 'Fail'
go.ERROR_NUM += 1
go.PARAMS = hp.str_get_param(gm[self.index]["param"][0], go.PARAMS) #把登陆成功后的token,userid合并到请求参数中
go.REALLY_RESULT = self.config_http.get(gm[self.index]["url"], go.PARAMS)
hope = ast.literal_eval(self.hope)
temp_result = check.compare(hope, go.REALLY_RESULT, gm[self.index]["isList"])

sum_test_data(temp_result) #统计
go.CASE_TOTAL += 1

# 获取测试套件
Expand All @@ -104,7 +114,7 @@ def get_test_suite(index):
# 运行测试用例函数
def run_case(runner):
case_list = httpbase.No
case_list = eval(case_list) # 把字符串类型的list转换为list
case_list = ast.literal_eval(case_list) # 把字符串类型的list转换为list
if len(case_list) == False: #判断是否执行指定的用例ID
temp_case = gm
for index in range(1, len(temp_case)):
Expand All @@ -120,9 +130,10 @@ def run_case(runner):
if str(i) == gm[j]['id']:
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"],
# 记录运行结果
mresult.info.append(json.loads(json.dumps(resultInfo(MresultDetail.resultInfo(), t_id=gm[j]["id"], t_name=gm[j]["name"], t_url=gm[0]["host"] +"/"+gm[j]["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()))
t_method=gm[j]["method"]).to_primitive())))

# 运行测试套件
if __name__ == '__main__':
Expand All @@ -141,7 +152,7 @@ def run_case(runner):
excel_report(workbook, data, worksheet, worksheet2)

# 发送email
get_email = get_email()
BsendEmail.send_mail(get_email)
# get_email = get_email()
# BsendEmail.send_mail(get_email)


8 changes: 4 additions & 4 deletions test4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<title>导购码接口测试</title>
<host>dgm.boweixin.com</host>
<port>80</port>
<No>[]</No>
<No>[1001]</No>
<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"}</header>
<InterfaceList>
<params>
<name type="str">account</name>
Expand All @@ -16,7 +17,7 @@
<must>1</must>
</params>
<params>
<name type="str">type</name>
<name type="int">type</name>
<value>0</value>
<must>1</must>
</params>
Expand All @@ -38,10 +39,9 @@
<id>1002</id>
<name>个人主页</name>
<method>GET</method>
<url>GetPersonalHomePage</url>
<url>GetPersonalHomePage1</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 f4d1178

Please sign in to comment.