Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yanchunhuo committed Apr 24, 2018
1 parent 23a274c commit 4f31cb4
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 38 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# 说明:
# 基于excel的接口自动化测试:

## 一、安装依赖库
* pip install -r requirements.txt

## 二、使用方法
* 修改auto_testing_excel目录中的case.xlsx的用例
* 修改auto_testing_excel目录中的config.py的相关配置
* 安装xlrd/xlwt/requests
* python \*/auto_testing_excel/test_main.py > myreport.html
* python run.py > myreport.html

## 三、软件
* unittest 单元测试
* HTMLTestRunner 将unittest结果生成html
* requests http请求库
* xlrd 读excel
* xlwt 写excel

## 三、使用的软件
* unittest 单元测试
* HTMLTestRunner=0.8.2 将unittest结果生成html
* requests=2.12.4 http请求
* xlrd=1.0.0 读excel
* xlwt=1.1.2 写excel
## 四、编码规范
* Python使用2.7版本
* 代码编写全部统一在Linux环境下进行
* 文件编码统一使用"-\*- coding:utf-8 -\*-"
File renamed without changes.
Empty file added cases/__init__.py
Empty file.
File renamed without changes.
17 changes: 17 additions & 0 deletions common/decorater.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/python
#-*- coding:utf-8 -*-
'''
该模块存放一些共用的修饰器
'''
#日志修饰器
def log(func):
def wrapper(*args,**kw):
print '调用函数的参数为:',args,kw
print '开始执行函数'
func(*args,**kw)
print '结束执行函数'
return wrapper


if __name__=='__main__':
print "手术室"
27 changes: 27 additions & 0 deletions common/strTools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/python
#-*- coding:utf-8 -*-
import uuid
'''
@description 获取一些资源
'''

'''
@description 获得唯一名称
@param preStr 加前缀
@return string
'''
def getUUID(preStr):
if(preStr=='' or preStr is None):
return 'autotesting_'+(str)(uuid.uuid1())
else:
return 'autotesting_'+str(preStr)+'_'+(str)(uuid.uuid1())

'''
@description 如果目录路径最后一位不是'/',则修改
@dirPath 需要修改的目录路径
@return string
'''
def amendDirPath(dirPath):
if(dirPath[-1]!='/'):
return dirPath+'/'
return dirPath
Empty file added config/__init__.py
Empty file.
17 changes: 9 additions & 8 deletions auto_testing_excel/config.py → config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@
#-*- coding:utf-8 -*-
import logging

#[http config]
'''[http config]'''
use_session=True

#[case config]
excel_file='/root/automatic_testing/auto_testing_excel/case.xlsx'
'''[case config]'''
excel_file='cases/cases.xlsx'
test_sheet_num=1

#[log config]
log_file='/root/automatic_testing/auto_testing_excel/auto_testing_excel.log'
fail_log_file='/root/automatic_testing/auto_testing_excel/auto_testing_excel_fail.log'
'''[log config]'''
log_file='logs/test.log'
fail_log_file='logs/test_fail.log'
formatter=logging.Formatter('%(asctime)s-%(filename)s-%(name)s-%(levelname)s : %(message)s')

#logger用于记录测试信息,包括测试成功、异常、错误
fileHandler=logging.FileHandler(log_file,'a')
#用于输出到屏幕
streamHandler=logging.StreamHandler()
fileHandler.setFormatter(formatter)
streamHandler.setFormatter(formatter)
logger=logging.getLogger('log')
logger.setLevel(logging.DEBUG)
logger.addHandler(fileHandler)
logger.addHandler(streamHandler)

#fail_logger用于记录测试错误,包括测试失败、异常、错误
fail_fileHandler=logging.FileHandler(fail_log_file,'a')
fail_fileHandler.setFormatter(formatter)
fail_logger=logging.getLogger('fail_log')
Expand Down
Empty file added logs/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests==2.12.4
xlrd==1.0.0
xlutils==2.0.0
xlwt==1.1.2
xlwt==1.1.2
42 changes: 24 additions & 18 deletions auto_testing_excel/test_main.py → run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import json
import re

import config
from config import logger,fail_logger
from automatic_testing.common import HTMLTestRunner
from automatic_testing.common.excelTools.excelOperator import ExcelTool
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():
Expand All @@ -19,7 +19,7 @@ def doTest():
method=case['method']
headers=case['headers']
if(headers is not None and headers.strip()!=''):
print 'headers::::::',headers
logger.info('case:'+str(uId)+',headers '+headers)
headers=json.loads(headers)
param=case['param']
if(param is not None and param.strip()!=''):
Expand Down Expand Up @@ -48,7 +48,7 @@ def doTest():
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())):
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())
Expand All @@ -59,27 +59,33 @@ def doTest():
fail_logger.error('case:'+str(uId)+',fail##request response None')
return doTest

class assertTools():
class AssertTools():
@staticmethod
def assertEqual(regular,source):
regular=regular.replace('[','\[')
regular=regular.replace(']','\]')
regular=regular.replace('*','\*')
pattern=re.compile(regular)
if(pattern.match(source)):
return True
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.log_file)
status,output=commands.getstatusoutput('>'+config.fail_log_file)
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.excel_file,config.test_sheet_num)
if(config.use_session):
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
Expand All @@ -96,5 +102,5 @@ def newFunc(self,*args,**kw):

if __name__=='__main__':
Test.init()
#HTMLTestRunner.main()
HTMLTestRunner.main()
unittest.main()

0 comments on commit 4f31cb4

Please sign in to comment.