python+requests接口自动化分层框架设计

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('查询接口调用失败')

原文地址:https://www.cnblogs.com/xiaolehua/p/14182314.html