-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun.py
106 lines (99 loc) · 4.27 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
#-*- coding:utf-8 -*-
import commands
import unittest
import requests
import json
import re
import config.config
from config.config import logger,fail_logger
from common import HTMLTestRunner
from common.excelTools.excelOperator import ExcelTool
def test(request_s,case):
def doTest():
uId=case['id']
name=case['name']
url=case['url']
method=case['method']
headers=case['headers']
if(headers is not None and headers.strip()!=''):
logger.info('case:'+str(uId)+',headers '+headers)
headers=json.loads(headers)
param=case['param']
if(param is not None and param.strip()!=''):
param=json.loads(param)
expect=case['expect']
r=None
logger.info('start testing '+str(uId))
#rquest
if(request_s is not None):
if(method=='post'):
r=request_s.post(url,data=param,headers=headers)
elif(method=='get'):
r=request_s.get(url,data=param,headers=headers)
else:
logger.error('case:'+str(uId)+',fail##the method \''+method+'\' is not support!')
fail_logger.error('case:'+str(uId)+',fail##the method \''+method+'\' is not support!')
raise ValueError('[the method \''+method+'\' is not support!]')
else:
if(method=='post'):
r=requests.post(url,data=param,headers=headers)
elif(method=='get'):
r=requests.get(url,data=param,headers=headers)
else:
logger.error('case:'+str(uId)+',fail##the method \''+method+'\' is not support!')
fail_logger.error('case:'+str(uId)+',fail##the method \''+method+'\' is not support!')
raise ValueError('[the method \''+method+'\' is not support!]')
#assert
if(r is not None):
if(AssertTools.assertEqual(expect.strip(),r.text.decode('unicode-escape').strip())):
logger.info('case:'+str(uId)+',success##status_code:'+str(r.status_code)+'##'+expect.strip()+' equal '+r.text.decode('unicode-escape').strip())
else:
logger.error('case:'+str(uId)+',fail##status_code:'+str(r.status_code)+'##'+expect.strip()+' not equal '+r.text.decode('unicode-escape').strip())
fail_logger.error('case:'+str(uId)+',fail##status_code:'+str(r.status_code)+'##'+expect.strip()+' not equal '+r.text.decode('unicode-escape').strip())
raise AssertionError(expect.strip()+' not equal '+r.text.decode('unicode-escape').strip())
else:
logger.error('case:'+str(uId)+',fail##request response None')
fail_logger.error('case:'+str(uId)+',fail##request response None')
return doTest
class AssertTools():
@staticmethod
def assertEqual(regular,source):
if(regular=='' or regular is None):
if(regular==source):
return True
return False
else:
regular=regular.replace('[','\[')
regular=regular.replace(']','\]')
regular=regular.replace('*','\*')
pattern=re.compile(regular)
if(pattern.match(source)):
return True
class Test(unittest.TestCase):
@classmethod
def init(self):
logger.info('init')
logger.info('clean logs')
status,output=commands.getstatusoutput('>' + config.config.log_file)
status,output=commands.getstatusoutput('>' + config.config.fail_log_file)
logger.info('read case from excel')
excelTool=ExcelTool()
self.cases=excelTool.readAsDictArray(config.config.excel_file,
config.config.test_sheet_num)
if(config.config.use_session):
self.s=requests.Session()
else:
self.s=None
logger.info('start to test')
for curt_case in range(len(self.cases)):
self.add_method('test'+str(self.cases[curt_case]['id']),test(self.s,self.cases[curt_case]))
@classmethod
def add_method(cls,func_name,func):
def newFunc(self,*args,**kw):
func(*args,**kw)
setattr(cls,func_name,newFunc)
if __name__=='__main__':
Test.init()
HTMLTestRunner.main()
unittest.main()