TA的每日心情 | 擦汗 昨天 09:33 |
---|
签到天数: 532 天 连续签到: 2 天 [LV.9]测试副司令
|
1. 项目结构是这样的。
2. 通用函数代码
- <font size="3">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)
- </font>
复制代码 3. 登陆代码
- <font size="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'])
- </font>
复制代码 4. 模块运行
- <font size="3"># 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()
- </font>
复制代码 5. 生成测试报告
- <font size="3">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
- </font>
复制代码 6. 测试报告还是很直观的。
|
|