def test_login(self,case): #准备用例数据 #获取url url = case["url"] #获取请求参数params params = eval(case['data']) #获取预期结果 expected = eval(case['expected']) #获取请求方法 request_method = case['method'] #获取请求头信息 headers = eval(config.get('request', 'headers')) #发起请求 response = requests.request(method=request_method,url=url,json = params,headers = headers) #获取返回信息 res = response.json() try: #断言,预期结果与实际结果比较 self.assertAlmostEqual(expected['code'],res["code"]) self.assertAlmostEqual(expected['msg'],res['msg']) except AssertionError as e : #错误的用例记录在logging日志中 mylog_test.error(f'用例{case["title"]}出现错误,错误信息为:{e}') mylog_test.exception(e) raise e else: mylog_test.info(f'用例运行正常{case["title"]}')
def setUpClass(cls): #前置登陆 login_url = config.get('login_test', 'url') params = { "mobile_phone": config.get('login_test', 'mobile_phone'), "pwd": config.get('login_test', 'password') } headers = eval(config.get('request', 'headers')) response = requests.request(method='post', json=params, headers=headers, url=login_url) res = response.json() cls.token = 'Bearer ' + jsonpath.jsonpath(res, "$..token")[0] cls.id = jsonpath.jsonpath(res, "$..id")[0]
def setUpClass(cls): url = config.get('login_test', 'url') params = { "mobile_phone": config.get('login_test', 'mobile_phone'), "pwd": config.get('login_test', 'password') } headers = eval(config.get('request', 'headers')) reponse = requests.request(url=url, method='post', json=params, headers=headers) res = reponse.json() cls.token = 'Bearer ' + jsonpath.jsonpath(res, "$..token")[0] cls.id = res['data']['id']
def test_register(self, case): #获取请求url地址 URL = config.get('request', 'url') + case['url'] #获取请求头 headers = eval(config.get('request', 'headers')) headers["Authorization"] = self.token #获取data params = eval(case['data']) if "#member#" in case['data']: params['member_id'] = params['member_id'].replace( "#member#", str(self.id)) #预期结果 excpted = eval(case['expected']) #获取请求方法 mothed = case['method'] sql = case["check_sql"] if sql: start_money = sql_data.find_data(sql.format(self.id)) s_money = start_money[0]['leave_amount'] #发送请求 request = requests.request(method=mothed, json=params, headers=headers, url=URL) res = request.json() if sql: end_money = sql_data.find_data(sql.format(self.id)) e_money = end_money[0]['leave_amount'] try: #比对预期结果与实际结果 self.assertEqual(excpted['code'], res['code']) if sql: self.assertEqual(float(e_money - s_money), params["amount"]) mylog_test.info(f"{case['title']}这条用例通过") except AssertionError as e: mylog_test.info(f'{case["title"]}这条用例未通过') raise e
def test_register(self, case): #获取url地址 url = config.get('request', 'url') + case["url"] #获取请求方法 method = case["method"] #获取头部信息 headers = eval(config.get('request', 'headers')) #判断手机号是否需要替换 if "*phone*" in case['data']: phone = self.get_phone() case['data'] = case['data'].replace("*phone*", phone) #获取注册信息 params = eval(case['data']) #获取预期结果 expected = eval(case["expected"]) #发送请求 response = requests.request(method=method, url=url, headers=headers, json=params) res = response.json() try: self.assertAlmostEqual(expected['code'], res['code']) #用例校验的sql写在excel中,获取sql sql = case["check_sql"] if sql: res1 = sql_data.find_data(sql.format(params['mobile_phone'])) self.assertTrue(res1) except AssertionError as e: mylog_test.error(f'{case["title"]}该条测试用例未通过') mylog_test.exception(e) raise e else: mylog_test.info(f'{case["title"]}该条用例通过')
def test_withdraw(self, case): #请求url url = config.get('request', 'url') + case['url'] #请求方法 method = case['method'] #请求data params = eval(case['data']) if "#member_id#" in case['data']: params['member_id'] = params['member_id'].replace( '#member_id#', str(self.id)) #请求头 headers = eval(config.get('request', 'headers')) headers["Authorization"] = self.token expected = eval(case['expected']) sql = case['check_sql'] if sql: start_money = sql_data.find_data(sql.format(self.id)) s_money = start_money[0]['leave_amount'] reponse = requests.request(method=method, json=params, headers=headers, url=url) res = reponse.json() try: if sql: end_money = sql_data.find_data(sql.format(self.id)) e_money = end_money[0]['leave_amount'] self.assertEqual(float(s_money - e_money), params['amount']) self.assertEqual(expected['code'], res['code']) mylog_test.info(f'用力执行通过{case["title"]}') except AssertionError as e: mylog_test.info(f'{case["title"]}执行失败') raise e pass
class TestWithDraw(unittest.TestCase): #读取excel数据 excel = Manage_Excel( os.path.join(DATA_DIR, config.get('excel', 'workbook')), config.get('excel', 'sheet_withdraw')) excel_data = excel.get_data() @classmethod def setUpClass(cls): #前置登陆 login_url = config.get('login_test', 'url') params = { "mobile_phone": config.get('login_test', 'mobile_phone'), "pwd": config.get('login_test', 'password') } headers = eval(config.get('request', 'headers')) response = requests.request(method='post', json=params, headers=headers, url=login_url) res = response.json() cls.token = 'Bearer ' + jsonpath.jsonpath(res, "$..token")[0] cls.id = jsonpath.jsonpath(res, "$..id")[0] @my_ddt.data(*excel_data) def test_withdraw(self, case): #请求url url = config.get('request', 'url') + case['url'] #请求方法 method = case['method'] #请求data params = eval(case['data']) if "#member_id#" in case['data']: params['member_id'] = params['member_id'].replace( '#member_id#', str(self.id)) #请求头 headers = eval(config.get('request', 'headers')) headers["Authorization"] = self.token expected = eval(case['expected']) sql = case['check_sql'] if sql: start_money = sql_data.find_data(sql.format(self.id)) s_money = start_money[0]['leave_amount'] reponse = requests.request(method=method, json=params, headers=headers, url=url) res = reponse.json() try: if sql: end_money = sql_data.find_data(sql.format(self.id)) e_money = end_money[0]['leave_amount'] self.assertEqual(float(s_money - e_money), params['amount']) self.assertEqual(expected['code'], res['code']) mylog_test.info(f'用力执行通过{case["title"]}') except AssertionError as e: mylog_test.info(f'{case["title"]}执行失败') raise e pass
from selenium.webdriver import Chrome from Common.INI_setting import config import time def login_test(username, password): driver = Chrome() driver.get("https://test-omo.aiyouyi.cn/bp/login") time.sleep(4) driver.find_element_by_xpath("//input[@name = 'userName']").send_keys( username) driver.find_element_by_xpath("//input[@name = 'password']").send_keys( password) driver.find_element_by_xpath( '//*[@id="app"]/div/form/div[2]/button').click() time.sleep(3) if __name__ == "__main__": login_test(username=config.get('login', 'user'), password=config.get('login', 'password'))
def __init__(self,host,port,user,password): self.sql_object = pymysql.connect( host = host, port = port, user= user, password = password, cursorclass = pymysql.cursors.DictCursor, charset = 'utf8' ) self.cur = self.sql_object.cursor() def find_data(self,sql): self.sql_object.commit() self.cur.execute(sql) data = self.cur.fetchall() return data sql_data = connect_sql(host= config.get("mysql",'host'), port=eval(config.get('mysql','port')), user=config.get('mysql','user'), password= config.get('mysql','password') )
import unittest import os import requests import random from Common.Excel import Manage_Excel from Common.handel_path import DATA_DIR from Common.INI_setting import config from Common.get_log import mylog_test from Common.handel_sql import sql_data from Common import my_ddt #读取excel数据 excel_data = Manage_Excel( os.path.join(DATA_DIR, config.get('excel', 'workbook')), config.get('excel', 'sheet_register')) case_data = excel_data.get_data() @my_ddt.ddt class TestRegister(unittest.TestCase): @my_ddt.data(*case_data) def test_register(self, case): #获取url地址 url = config.get('request', 'url') + case["url"] #获取请求方法 method = case["method"] #获取头部信息 headers = eval(config.get('request', 'headers'))
class TestRecharge(unittest.TestCase): excel = Manage_Excel( os.path.join(DATA_DIR, config.get('excel', 'workbook')), config.get('excel', 'sheet_recharge')) # excel_data = Manage_Excel(os.path.join(DATA_DIR, config.get('excel', 'workbook')), # config.get('excel', 'sheet_register')) excel_data = excel.get_data() @classmethod def setUpClass(cls): url = config.get('login_test', 'url') params = { "mobile_phone": config.get('login_test', 'mobile_phone'), "pwd": config.get('login_test', 'password') } headers = eval(config.get('request', 'headers')) reponse = requests.request(url=url, method='post', json=params, headers=headers) res = reponse.json() cls.token = 'Bearer ' + jsonpath.jsonpath(res, "$..token")[0] cls.id = res['data']['id'] @my_ddt.data(*excel_data) def test_register(self, case): #获取请求url地址 URL = config.get('request', 'url') + case['url'] #获取请求头 headers = eval(config.get('request', 'headers')) headers["Authorization"] = self.token #获取data params = eval(case['data']) if "#member#" in case['data']: params['member_id'] = params['member_id'].replace( "#member#", str(self.id)) #预期结果 excpted = eval(case['expected']) #获取请求方法 mothed = case['method'] sql = case["check_sql"] if sql: start_money = sql_data.find_data(sql.format(self.id)) s_money = start_money[0]['leave_amount'] #发送请求 request = requests.request(method=mothed, json=params, headers=headers, url=URL) res = request.json() if sql: end_money = sql_data.find_data(sql.format(self.id)) e_money = end_money[0]['leave_amount'] try: #比对预期结果与实际结果 self.assertEqual(excpted['code'], res['code']) if sql: self.assertEqual(float(e_money - s_money), params["amount"]) mylog_test.info(f"{case['title']}这条用例通过") except AssertionError as e: mylog_test.info(f'{case["title"]}这条用例未通过') raise e
# _*_coding:utf-8_*_ # @time :2020/11/30 6:03 下午 # @Author :hanjiping # Emil :[email protected] # File :gegister.py # @software PyCharm import requests from Common.INI_setting import config json_data = { "mobile_phone":"15011466717", "pwd":"12345678" } headers =eval(config.get('request','headers')) response = requests.request(method='post',headers = headers,json = json_data,url = "http://api.lemonban.com/futureloan/member/register" ) print(response.json())