python+requests接口自動化分層框架設計
cms_api==========》存放所有組建接口的請求
config===========》存放所有的接口入參
report===========》存放測試報告
run=============》存放自動搜索執行用例+生成測試報告
testcast==========》存放所有的接口用例
utils============》存放所有的工具
1、cms_api(目錄)/all_api.py:
'''此模塊存放所有組建接口的請求'''
import requests
from config.parameter import *
class Api(object):
def __init__(self):
self.s = requests.session()
'''登錄接口請求'''
def login(self):
r1 = self.s.request('post',url=login_url,data=login_parameter)
return r1.json()
'''查詢接口請求'''
def select(self):
r2 = self.s.request('get',url=select_url,params=select_parameter)
return r2.json()
2、config(目錄)/parameter.py:
'''此模塊存放所有接口入參'''
#①、登錄接口所有入參
login_url = 'http://192.168.127.191:8081/cms/manage/loginJump.do'
login_parameter = {
'userAccount': 'admin',
'loginPwd': 123456
}
#②、查詢接口所有入參
select_url = 'http://192.168.127.191:8081/cms/manage/queryUserList.do'
select_parameter = {
'startCreateDate':'',
'endCreateDate':'',
'searchValue':'',
'page': 1
3、run(目錄)/run_case.py:
'''此模塊自動搜索用例執行且生成測試報告和發送測試報告到郵箱'''
from utils.HTMLTestRunner3_New import HTMLTestRunner
import unittest
from utils.mail3 import SendMail
class Action:
def auto_test(self):
path1 = 'E:\python project2\\testcase'
filename = 'E:\python project2\\report'+'\\'+'api.html'
f = open(filename,'wb')
discover = unittest.defaultTestLoader.discover( start_dir=path1,pattern='api_test.py')
runner = HTMLTestRunner(stream=f,title='接口自動化測試報告', description='用例執行情況如下',tester='令狐沖')
runner.run(discover)
f.close()
if __name__ == '__main__':
t = Action()
t.auto_test()
4、testcase(目錄)/api_test.py:
'''此模塊存放所有的用例'''
import requests
import unittest
from cms_api.all_api import Api
class All_api(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.r = Api()
def test_login(self):
result = self.r.login() #調用登錄接口請求返回一個結果用result來接收
if result['code'] == '200':
print('登錄接口調用成功')
else:
print('登錄接口調用失敗')
def test_select(self):
result = self.r.select() #調用登錄接口請求返回一個結果用result來接收
if result['code'] == '200':
print('查詢接口調用成功')
else:
print('查詢接口調用失敗')