微服务平台API测试

栏目: 后端 · 发布时间: 5年前

内容简介:1.项目结构是这样的。2.通用函数代码
编辑推荐:
本文来自于csdn,本文介绍了在微服务平台API测试过项目结构和代码过程等相关内容,希望对您的学习能有所帮助。

1.项目结构是这样的。

微服务平台API测试

2.通用函数代码

import json,pymysql,collections
 
 def read_json(path):
 with open(path,'r',encoding = 'utf-8') as load_f:
 load_dict = json.load(load_f)
 return load_dict
 
 def read_sql():
 db = pymysql.connect(db='chameleon_user', host='172.16.78.71', port=3306, user='root', passwd='1qazxsw2',charset='utf8')
 cursor = db.cursor()
 sql = 'select * from `%s`' % ('role')
 cursor.execute(sql)
 rows = cursor.fetchall()
 objects_list = []
 for row in rows:
 d = collections.OrderedDict()
 d['cardnum'] = row[0]
 d['bankname'] = row[1]
 d['phonenumber'] = row[2]
 d['rest'] = row[3]
 d['id_main'] = row[4]
 objects_list.append(d)
 
 j = json.dumps(objects_list)
 return j
 
 if __name__ == "__main__":
 json_from_sql = read_sql()
 print(json_from_sql)

3. 登陆代码

import requests
 import json
 
 class login():
 headers = {'Content-Type': 'application/json;charset=utf-8',
 'token': 'planceholder',
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
 'userId': 'planceholder'
 }
 
 def __init__(self):
 self.login_url = 'http://172.16.78.72:8301/v1/chameleon-user/user/login'
 self.form_data = {'account':'admin',
 'password':'zs123YL!'}
 
 
 def get_login_token(self):
 try:
 r = requests.post(self.login_url,headers=self.headers,data = json.dumps(self.form_data))
 login_return_dicts = r.json()
 self.headers['token'] = login_return_dicts['token']
 self.headers['userId']= str(login_return_dicts['data']['userId'])
 #print(login_return_dicts)
 except:
 print('登陆平台失败!获取Token失败')
 return
 
 if __name__ == '__main__':
 l = login()
 l.get_login_token()
 print(login.headers['token'])
 print(login.headers['userId'])

4.模块运行

# coding=utf-8
 import requests,sys
 import unittest
 import json
 from common.read_file import read_json as readjson
 from common.login_platform import login as login
 
 login = login()
 login.get_login_token()
 
 class RunInstance(unittest.TestCase):
 def setUp(self):
 self.headers = login.headers
 self.instance_data = readjson(r'C:\Users\wenhuifu\ PycharmProjects\xu_api\Data\cloudmanager \RunInstance.json')
 self.url = 'http://172.16.78.73:3011/api /chameleon-cvg/instance'
 
 #@unittest.skip('不测试!')
 def test_run_instance(self):
 try:
 r = requests.post(self.url,headers = self.headers,json = self.instance_data)
 print(r.json())
 except:
 #print(err)
 print('创建机器失败!')
 
 assert r.status_code == 200
 
 def tearDown(self):
 pass
 
 
 if __name__ == '__main__':
 create = RunInstance()
 create.test_run_instance()

5.生成测试报告

from HTMLTestRunner_PY3 import HTMLTestRunner
 from email.mime.text import MIMEText
 from email.header import Header
 import smtplib
 import unittest
 import time
 import os
 
 #发送邮件
 def send_mail(file_new):
 f = open(file_new,'rb')
 mail_body = f.read()
 f.close()
 
 msg = MIMEText(mail_body,'html','utf-8')
 msg['Subject'] = Header("自动化测试报告",'utf-8')
 
 smtp = smtplib.SMTP()
 smtp.connect('smtp.126.com')
 smtp.login("username@126.com","123456")
 smtp.sendmail("username@126.com", "receive@126.com",msg.as_string())
 smtp.quit()
 print('Email has been send out!')
 
 def new_report(testreport):
 lists = os.listdir(testreport)
 lists.sort(key = lambda fn:os.path . getmtime (testreport + "\\" + fn))
 file_new = os.path.join(testreport,lists[-1])
 print(file_new)
 return file_new
 
 if __name__ == "__main__":
 now = time.strftime("%Y-%m-%d %H_%M_%S")
 filename = './Report/' + now + ' result.html'
 fp = open(filename,'wb')
 runner = HTMLTestRunner(stream=fp,title = 'XU API Test Result',description= '环境 Windows 10')
 discover = unittest.defaultTestLoader.discover ('./Testcase',pattern= '*_case.py')
 runner.run(discover)
 fp.closed

6.测试报告还是很直观的。

微服务平台API测试


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python算法教程

Python算法教程

[挪威] Magnus Lie Hetland 赫特兰 / 凌杰、陆禹淳、顾俊 / 人民邮电出版社 / 2016-1-1 / 69.00元

本书用Python语言来讲解算法的分析和设计。本书主要关注经典的算法,但同时会为读者理解基本算法问题和解决问题打下很好的基础。全书共11章。分别介绍了树、图、计数问题、归纳递归、遍历、分解合并、贪心算法、复杂依赖、Dijkstra算法、匹配切割问题以及困难问题及其稀释等内容。本书在每一章结束的时候均有练习题和参考资料,这为读者的自我检查以及进一步学习提供了较多的便利。在全书的最后,给出了练习题的提......一起来看看 《Python算法教程》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具