Commit 070ce285d7381a559203e66bef089e295f98dcec

Authored by lixi
1 parent 2bb52757

更新文件

commons/MySession.py
1 1 #!/usr/bin/python
2 2 # -*- coding: UTF-8 -*-
3 3 import requests
  4 +import re
4 5 from commons import common as com
5 6 from commons.Logging import log
6 7 from commons.clientSession import cliSession
... ... @@ -36,16 +37,17 @@ class mysession(requests.Session):
36 37 body = "userName=sg_wenze&password=111111"
37 38 body_client= {"userName":"sg_wenze","password":"111111"}
38 39  
39   - def __init__(self):
  40 + def __init__(self,host=None):
40 41 "如下代码,可以通过配置文件来控制测试环境和灰度环境,http和https"
41 42 super().__init__()
  43 + self.user={}
42 44 self.url = mysession.url.replace("http://test.", com.get_global_config("global_data", "environment", "en"))
43 45 self.header = mysession.header
44 46 self.body = mysession.body
45 47 self.url_client = mysession.url_client.replace("http://test.", com.get_global_config("global_data", "environment", "en"))
46 48 self.header_client = mysession.header_client
47 49 self.body_client = mysession.body_client
48   - self.timeout = (6,6)
  50 + self.timeout = (10,10)
49 51 self.max_retries = 3
50 52 self.keep_alive = False
51 53 self.ssl_verify = False
... ... @@ -55,36 +57,48 @@ class mysession(requests.Session):
55 57 self.allow_redirects = False
56 58 self.firmid={"group":"1","hd":"2","cd":"3","qqhe":"4","mdj":"5","gy":"6","cc":"7","sg":"8","sy":"9"}
57 59 self.market={"sy":"沈阳","heb":"哈尔滨","sg":"寿光","gy":"贵阳","cc":"长春","hs":"杭水","hg":"杭果"}
58   - self.user={}
  60 + self.host= None if host==None else eval(com.get_global_config("global_data", "host_ip", host))
  61 +
59 62  
60 63 def cliLogin(self,user="sy_userName_01"):
61 64 self.webHeaders, self.clientHeaders, self.userInfo = cliSession().loginUser(user=user)
62 65 return self
63 66  
64   - def get_session(self, account, **kwargs):
65   - "如下代码,可以通过配置文件来控制登录的账户session"
66   - self.body = self.body.replace("sg_wenze",
67   - com.get_global_config("global_data", "account", account).split("&")[0])
68   - self.body = self.body.replace("111111",
69   - com.get_global_config("global_data", "account", account).split("&")[1])
70   - self.se = requests.session()
71   - co = requests.cookies.RequestsCookieJar()
72   - #加入UAP_firmId属性
73   - firm=account.split("_")[0]
74   - co.set("UAP_firmId", self.firmid[firm])
75   - self.se.cookies.update(co)
76   - # 进行登录请求
77   - re = self.se.post(url=self.url, headers=self.header, data=self.body, proxies=self.proxies, **kwargs)
78   - self.UAP_accessToken=self.se.cookies["UAP_accessToken"]
79   - self.UAP_refreshToken=self.se.cookies["UAP_refreshToken"]
80   - return self.se
  67 + # def get_session(self, account, **kwargs):
  68 + # "如下代码,可以通过配置文件来控制登录的账户session"
  69 + # self.body = self.body.replace("sg_wenze",
  70 + # com.get_global_config("global_data", "account", account).split("&")[0])
  71 + # self.body = self.body.replace("111111",
  72 + # com.get_global_config("global_data", "account", account).split("&")[1])
  73 + # self.se = requests.session()
  74 + # co = requests.cookies.RequestsCookieJar()
  75 + # #加入UAP_firmId属性
  76 + # firm=account.split("_")[0]
  77 + # co.set("UAP_firmId", self.firmid[firm])
  78 + # self.se.cookies.update(co)
  79 + # # 进行登录请求
  80 + # re = self.se.post(url=self.url, headers=self.header, data=self.body, proxies=self.proxies, **kwargs)
  81 + # self.UAP_accessToken=self.se.cookies["UAP_accessToken"]
  82 + # self.UAP_refreshToken=self.se.cookies["UAP_refreshToken"]
  83 + # return self.se
  84 + #
  85 + # def get_login_info(self, account, **kwargs):
  86 + # "用于获取用户信息"
  87 + # self.body_client.update({"userName":com.get_global_config("global_data", "account", account).split("&")[0]})
  88 + # self.body_client.update({"password":pwdCry(com.get_global_config("global_data", "account", account).split("&")[1])})
  89 + # tmp = requests.post(url=self.url_client, headers=self.header_client, json=self.body_client, proxies=self.proxies, **kwargs)
  90 + # return tmp
81 91  
82   - def get_login_info(self, account, **kwargs):
83   - "用于获取用户信息"
84   - self.body_client.update({"userName":com.get_global_config("global_data", "account", account).split("&")[0]})
85   - self.body_client.update({"password":pwdCry(com.get_global_config("global_data", "account", account).split("&")[1])})
86   - tmp = requests.post(url=self.url_client, headers=self.header_client, json=self.body_client, proxies=self.proxies, **kwargs)
87   - return tmp
  92 + def url_pro(self, url, host):
  93 + # url = url.replace(" ", "")
  94 + if host!=None:
  95 + if "http:" in url:
  96 + d1 = re.match(r"http://(.+?)/", url).group(1).split(":")[0]
  97 + url = re.sub(r"http://(.+?)/", r"http://" + host[d1] + "/", url)
  98 + elif "https:" in url:
  99 + d1 = re.match(r"https://(.+?)/", url).group(1).split(":")[0]
  100 + url = re.sub(r"https://(.+?)/", r"https://" + host[d1] + "/", url)
  101 + return url
88 102  
89 103 def get_session_client(self, account, **kwargs):
90 104 "get_session和get_session_client的方法只能用一个"
... ... @@ -151,6 +165,8 @@ class mysession(requests.Session):
151 165 """
152 166 # 记录日志
153 167 log.info("{0:=^86}".format(''))
  168 + log.info(url)
  169 + url=self.url_pro(url,self.host)
154 170 log.info("{}\n{}\n".format(url, kwargs))
155 171 # 进行请求
156 172 re = super().request(method , url, **kwargs,timeout=self.timeout)
... ... @@ -262,16 +278,15 @@ class mysession(requests.Session):
262 278  
263 279 my = mysession()
264 280 my.set_mark()
265   -
266 281 # 沈阳客户端session
267   -sessionSy = mysession().cliLogin("sy_userName_01")
  282 +# sessionSy = mysession().cliLogin("sy_userName_01")
268 283 # 哈尔滨客户端session
269   -sessionHeb = mysession().cliLogin("hd_userName_01")
270   -# print(sessionSy.userInfo)
271   -# print(sessionHeb.userInfo)
272   -
273   -# sy1=mysession().get_session_client("sy_user01")
274   -# heb=mysession().get_session_client("heb_user01")
275   -hg = mysession().get_session_client("hg_user01")
276   -# sg=mysession().get_session_client("sg_user01")
277   -
  284 +# sessionHeb = mysession().cliLogin("hd_userName_01")
  285 +#获取对应市场session
  286 +# sy1=mysession("host1").get_session_client("sy_user01")
  287 +# heb=mysession("host1").get_session_client("heb_user01")
  288 +hg=mysession("host2").get_session_client("hg_user01")
  289 +# 检测登录接口
  290 +# sy1.check_login("sy_user01")
  291 +# 检测登录接口
  292 +hg.check_login("hg_user01")
278 293 \ No newline at end of file
... ...
config/global_data.conf
... ... @@ -24,8 +24,8 @@ user03=256
24 24  
25 25 [email]
26 26 #为空时[]不发邮件,若要发邮件,参考demo
27   -to_list=["lixi@diligrp.com","liujiqiang@diligrp.com","wenleiming@diligrp.com"]
28   -cc_list=["demo@diligrp.com"]
  27 +to_list=["wenleiming@diligrp.com"]
  28 +cc_list=["lixi@diligrp.com","liujiqiang@diligrp.com","wenleiming@diligrp.com"]
29 29 demo=["lixi@diligrp.com","liujiqiang@diligrp.com","wenleiming@diligrp.com","tg@diligrp.com"]
30 30  
31 31 [mark]
... ... @@ -35,7 +35,7 @@ demo=["heb1","P3","v1.6","沈阳",None]
35 35  
36 36  
37 37 [environment]
38   -#格式只能为一下几种
  38 +#格式只能为一下几种,用于区分环境
39 39 #http://test.
40 40 #https://test.
41 41 #http://
... ... @@ -57,8 +57,54 @@ gateway=test.gateway.diligrp.com:8285
57 57  
58 58  
59 59  
60   -
61   -
62   -
63   -
  60 +[host_ip]
  61 +#重构host
  62 +host1={
  63 + "test.customer.diligrp.com": "10.35.100.45:8382",
  64 + "test.bd.diligrp.com": "10.35.100.45:8384",
  65 + "test.jmsf.diligrp.com": "10.35.100.44:8385",
  66 + "test.account.diligrp.com": "10.35.100.44:8186",
  67 + "test.card.diligrp.com": "10.35.100.44:8386",
  68 + "test.trading.diligrp.com": "10.35.100.47:8387",
  69 + "test.logger.diligrp.com": "10.35.100.47:8283",
  70 + "test.orders.diligrp.com": "10.35.100.47:8185",
  71 + "test.rule.diligrp.com": "10.35.100.54:8284",
  72 + "test.gateway.diligrp.com": "10.35.100.54:8285",
  73 + "test.message.diligrp.com": "10.35.100.54:8289",
  74 + "test.dfs.diligrp.com": "10.35.100.55:9527",
  75 + "test.report.diligrp.com": "10.35.100.56:8388",
  76 + "test.exporter.diligrp.com": "10.35.100.56:8288",
  77 + "test.appmanager.diligrp.com": "10.35.100.56:9000",
  78 + "test.uid.diligrp.com": "10.35.100.56:8282",
  79 + "test.nacos.diligrp.com": "10.35.100.37:8848",
  80 + "test.scheduler.diligrp.com": "10.35.100.47:8281",
  81 + "test.bpmc.diligrp.com": "10.35.100.54:8617",
  82 + "test.route.diligrp.com": "10.35.100.54:8286",
  83 + "test.uap.diligrp.com": "10.35.100.55:80",
  84 + "test.trace.diligrp.com": "10.35.100.55:8082",
  85 + "test.xorder.diligrp.com": "10.35.100.56:8090"}
  86 +
  87 +#杭果host
  88 +host2={
  89 + "test.uap.diligrp.com": "10.28.12.150:80",
  90 + "test.bpmc.diligrp.com": "10.28.12.150:8617",
  91 + "test.as.diligrp.com": "10.28.12.150",
  92 + "test.logger.diligrp.com": "10.28.11.149:8283",
  93 + "test.scheduler.diligrp.com": "10.28.11.149",
  94 + "test.account.diligrp.com": "10.28.11.149:8186",
  95 + "test.rule.diligrp.com": "10.28.10.127:8284",
  96 + "test.bd.diligrp.com": "10.28.10.127",
  97 + "test.customer.diligrp.com": "10.28.10.127:8384",
  98 + "test.report.diligrp.com": "10.28.10.127:8388",
  99 + "test.jmsf.diligrp.com": "10.28.10.127:8385",
  100 + "test.uid.diligrp.com": "10.28.11.180:8282",
  101 + "test.message.diligrp.com": "10.28.11.190:8289",
  102 + "test.dfs.diligrp.com": "10.28.11.190:9527",
  103 + "test.gateway.diligrp.com": "10.28.10.159:8285",
  104 + "test.route.diligrp.com": "10.28.10.159:8286",
  105 + "test.card.diligrp.com": "10.28.10.159:8386",
  106 + "test.exporter.diligrp.com": "10.28.11.183",
  107 + "test.ia.diligrp.com": "10.28.11.183",
  108 + "test.settlement.diligrp.com": "10.28.11.183:8383",
  109 + "test.hg.diligrp.com": "10.28.12.239"}
64 110  
... ...
... ... @@ -16,69 +16,67 @@ from commons.scripts import delReport
16 16  
17 17 def Create_Testcase_suite(path=""):
18 18 '''创建测试套件'''
19   - if path!="":
20   - path+="/"
  19 + if path != "":
  20 + path += "/"
21 21 print(path)
22   - testunit=unittest.TestSuite()
  22 + testunit = unittest.TestSuite()
23 23 test_Loader = DiscoveringTestLoader()
24   - discover=test_Loader.discover("./testcase/{}".format(path),pattern='test_*.py',top_level_dir=None)
  24 + discover = test_Loader.discover("./testcase/{}".format(path), pattern='test_*.py', top_level_dir=None)
25 25 # print(discover)
26 26 for test_suite in discover:
27 27 testunit.addTests(test_suite)
28   -# print(testunit)
  28 + # print(testunit)
29 29 return testunit
30 30  
31   -
32   -def Run_Testcase(testsuit,head=""):
  31 +
  32 +def Run_Testcase(testsuit, head=""):
33 33 '''运行测试用例并生成报告'''
34 34 if (head in my.market.keys()):
35   - head="["+ my.market[head] + "]"
36   - now = time.strftime("%Y-%m-%d %H_%M_%S",time.localtime())
37   - path=os.path.dirname(os.path.abspath(sys.argv[0]))
38   - report_file=path+"/report/"+now+"_result.html"
39   - #创建报告文件
40   - fp=open(report_file,'wb')
  35 + head = "[" + my.market[head] + "]"
  36 + now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())
  37 + path = os.path.dirname(os.path.abspath(sys.argv[0]))
  38 + report_file = path + "/report/" + now + "_result.html"
  39 + # 创建报告文件
  40 + fp = open(report_file, 'wb')
41 41  
42   - runner=HTMLTestRunner_cn.HTMLTestRunner(
  42 + runner = HTMLTestRunner_cn.HTMLTestRunner(
43 43 stream=fp,
44 44 title=u'进门收费{}--接口测试报告'.format(head),
45 45 description=u'用例简要执行情况如下:(注:报告详细信息需要下载report.html并用浏览器打开)',
46   - verbosity = 2)
47   - #执行用例
  46 + verbosity=2)
  47 + # 执行用例
48 48 runner.run(testsuit)
49   - #关闭文件
  49 + # 关闭文件
50 50 fp.close()
51 51 delReport.delReport(path)
52 52 return report_file
53   -
  53 +
  54 +
54 55 def Send_email(filename):
55 56 '''判断邮件发送逻辑'''
56   - l=eval(com.get_global_config("global_data", "email","to_list").lower())
57   - c=eval(com.get_global_config("global_data", "email","cc_list").lower())
58   - if type(l)!=type([]):
  57 + l = eval(com.get_global_config("global_data", "email", "to_list").lower())
  58 + c = eval(com.get_global_config("global_data", "email", "cc_list").lower())
  59 + if type(l) != type([]):
59 60 raise Exception("error,pls input list type send-email address")
60   - elif len(l)==0:
  61 + elif len(l) == 0:
61 62 print("\n To_list of send-email is null,won't send email!")
62   - elif len(l)!=0:
  63 + elif len(l) != 0:
63 64 for i in l:
64 65 print("\n check send-email format : {}".format(i))
65   - if re.match(r'^[0-9a-zA-Z_]{1,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$',i):
  66 + if re.match(r'^[0-9a-zA-Z_]{1,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$', i):
66 67 pass
67 68 else:
68 69 raise Exception("error,pls check your send-email format")
69   - #发送邮件
70   - em.send_email(filename,send_to=l,cc_to=c)
  70 + # 发送邮件
  71 + em.send_email(filename, send_to=l, cc_to=c)
71 72 else:
72 73 print("\n Haven't sent the email,pls check send-email address!")
73   -
74 74  
75 75  
76 76 if __name__ == "__main__":
77   - #检测数据库
  77 + # 检测数据库
78 78 db.mysql_conn_test()
79   - #检测登录接口
80   - my.check_login("sy_user01")
81   - #创建测试套,运行测试用例,生成报告
82   - report=Run_Testcase(Create_Testcase_suite("sy"))
83   - #发送邮件
84   - Send_email(report)
  79 + # 创建测试套,运行测试用例,生成报告
  80 + report = Run_Testcase(Create_Testcase_suite())
  81 + # 发送邮件
  82 + Send_email(report)
85 83 \ No newline at end of file
... ...