def setUp(self): self.j = JMESPathExtractor() logger.info('开始执行测试前准备的数据,调用test_Xjx_login方法') self.xjx_login = Test_Xjx_login().test_Xjx_login() self.data['sessionid'] = self.xjx_login logger.info('123测试前准备的数据为:{0}'.format(self.data)) # 获取json测试数据 self.jsondata = self.test_data # 获取headers数据 self.j = JMESPathExtractor() self.test_headers = JsonConfig( path='wholesale', jsonpath='borrowMoney.json').get_jsondata(element='headers') # self.headers = self.j.addextract(query='headers', body=test_headers) self.test_headers['sessionid'] = self.xjx_login logger.info("123测试前准备的headers为:{0}".format(self.test_headers)) self.get_dbdata = database() select_userBankCardId = "SELECT id FROM user_card_info WHERE user_id = '768093098' ORDER BY id DESC LIMIT 1" logger.info('查询数据库数据的sql为:{0}'.format(select_userBankCardId)) db_userBankCardId_id = self.get_dbdata.fetch_one(select_userBankCardId) logger.info('查询数据库的数据,返回的order_id为:{0}'.format(db_userBankCardId_id)) json_str = json.dumps(db_userBankCardId_id) self.one_db_userBankCardId_id = self.j.extract(query='id', body=json_str) logger.info('one_db_userBankCardId_id的数据为:{0}'.format( self.one_db_userBankCardId_id)) self.jsondata['userBankCardId'] = self.one_db_userBankCardId_id logger.info('123data数据为:{0}'.format(self.jsondata))
def setUp(self): self.j = JMESPathExtractor() logger.info('开始执行测试前准备的数据,调用test_Xjx_login方法') self.xjx_login = Test_Xjx_login().test_Xjx_login() self.data['sessionid'] = self.xjx_login logger.info('测试前准备的数据为:{0}'.format(self.data)) # 获取json配置文件数据 self.jsondata = self.data
def getJson(self, GTId): self.params['GTId'] = GTId client = HTTPClient(url=self.url, method='POST', headers=self.headers) res = client.send(params=self.params) j = JMESPathExtractor() VedioList = j.extract(query='VedioList[]', body=res.text) PicList = j.extract(query='PicList[]', body=res.text) return PicList, VedioList
def __init__(self, path='public', jsonpath='public_config.json'): """ :param jsonpath: 配置文件的路径 """ JSONCONFIG_FILE = os.path.join(BASE_PATH, 'data', path, jsonpath) self._j = JMESPathExtractor() self._jsonConfig = YamlReader(JSONCONFIG_FILE).data self._content = open(JSONCONFIG_FILE, encoding='utf-8')
def setUp(self): self.j = JMESPathExtractor() logger.info('开始执行测试前准备的数据,调用test_Xjx_login方法') self.xjx_login = Test_Xjx_login().test_Xjx_login() self.data['sessionid'] = self.xjx_login logger.debug('测试前准备的数据为:{0}'.format(self.data)) # 获取json配置文件数据 self.jsondata = self.data self.j = JMESPathExtractor() self.get_dbdata = database() self.jsondata['money'] = self.borrow_money self.jsondata['period'] = self.period
class Test_Xjx_login(unittest.TestCase): URL = Config().get('URL') logger.info('请求的URL为:{0}'.format(URL)) LOGIN_URL = Config().get('login_url') LOGIN = URL + LOGIN_URL def test_Xjx_login(self): self.j = JMESPathExtractor() self.jsondata = JsonConfig() # 获取json配置文件数据 jsondata = JsonConfig().get_jsondata() logger.info('开始执行app登录接口,caseName:test_Xjx_Login') self.client = HTTPClient(url=self.LOGIN, method='POST') logger.info('请求的api路径为:{0}'.format(self.LOGIN_URL)) logger.info('拼接后的请求路径为:{0}'.format(self.LOGIN)) res = self.client.send(data=jsondata) logger.info('接口入参为:{0}'.format(jsondata)) self.assertIn('登录成功', res.text) self.sessionid = self.j.extract(query='data.item.sessionid', body=res.text) jsondata['sessionid'] = self.sessionid logger.info('该用户的sessionid为:{0}'.format(self.sessionid)) # print('该用户的sessionid为:{0}'.format(self.sessionid)) return self.sessionid
class JsonConfig(): """ 获取json配置文件方法 """ def __init__(self, path='public', jsonpath='public_config.json'): """ :param jsonpath: 配置文件的路径 """ JSONCONFIG_FILE = os.path.join(BASE_PATH, 'data', path, jsonpath) self._j = JMESPathExtractor() self._jsonConfig = YamlReader(JSONCONFIG_FILE).data self._content = open(JSONCONFIG_FILE, encoding='utf-8') def get_jsondata(self, element=None): """ :param element: 需要获取json的对应key;如data.item.loginStatus;此字段为空时,默认返回字符串中所有内容 :return: 返回对应的字符串数据 """ if element == None: self.rights = json.load(self._content) else: self._all_rights = json.load(self._content) # 把self._all_rights的值转成字符串,这样下面的self._j.extract方法才能识别 self.contents = json.dumps(self._all_rights) self.rights = self._j.extract(query=element, body=self.contents) return self.rights
class Test_Xjx_Index(unittest.TestCase): URL = Config().get('URL') logger.info('请求的URL为:{0}'.format(URL)) INDEX_URL = Config().get('index_url') INDEX = URL + INDEX_URL data = JsonConfig().get_jsondata() logger.info('data数据为:{0}'.format(data)) def setUp(self): self.j = JMESPathExtractor() logger.info('开始执行测试前准备的数据,调用test_Xjx_login方法') self.xjx_login = Test_Xjx_login().test_Xjx_login() self.data['sessionid'] = self.xjx_login logger.info('测试前准备的数据为:{0}'.format(self.data)) # 获取json配置文件数据 self.jsondata = self.data def test_xjx_http_index1(self): logger.info('开始执行app首页接口,caseName:test_xjx_http_index1') self.client = HTTPClient(url=self.INDEX, method='GET') logger.info('请求的api路径为:{0}'.format(self.INDEX_URL)) logger.info('拼接后的请求路径为:{0}'.format(self.INDEX)) res = self.client.send(params=self.data) logger.info('返回的参数为:{0}'.format(res.text)) logger.info('接口入参为:query--{0}'.format(self.data)) self.assertIn('访问首页成功', res.text) # data.item.loginStatus的值为登录标识,1为已登录;0为未登录 login_status = self.j.extract(query='data.item.loginStatus', body=res.text) logger.info('login_status的值为:{0}'.format(login_status)) # print(login_status) # 判断login_status的值是否是1 self.assertEqual(login_status, '1') return self.data
from utils.client import TCPClient import unittest from utils.config import Config from utils.extractor import JMESPathExtractor je = JMESPathExtractor() class TestAdd(unittest.TestCase): def setUp(self): c = Config() ip = c.get('ip') port = c.get('port') self.client = TCPClient(ip, port) def tearDown(self): self.client.close() def test_add(self): data = { 'action': 'add', 'params': {'a': 1, 'b': 2} } res = self.client.send(data, dtype='json') self.assertEqual(je.extract('jkresult', res), 3) self.assertEqual(je.extract('action', res), 'add') def test_wrong_action(self): data = { 'action': 'sub',
from utils.client import HTTPClient from utils.extractor import JMESPathExtractor url = 'http://server.goteaming.com.cn/Player_Base/Game/GetTeamDataSource' headers = { 'User-Agent': 'Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255' } client = HTTPClient(url=url, method='POST', headers=headers) params = { 'GameId': 'e6693b01-7b38-4413-a991-1d268190f190', 'GTId': 'a840bc77-07b8-4d49-9047-22176dda6986' } res = client.send(params=params) print(res.text) j = JMESPathExtractor() VedioList = j.extract(query='VedioList[]', body=res.text) PicList = j.extract(query='PicList[]', body=res.text)
class Test_Xjx_BorrowMoney(unittest.TestCase): URL = Config().get('URL') logger.info('请求的URL为:{0}'.format(URL)) BorrowMoney_URL = Config().get('borrowMoney_url') addBorrowMoney_URL = Config().get('addBorrowMoney_url') data = JsonConfig().get_jsondata() logger.info('data数据为:{0}'.format(data)) get_jsondata = Config(path='driblet', config='borrowMoney.yml') borrow_money = get_jsondata.get('borrowMoney') period = get_jsondata.get('period') def setUp(self): self.j = JMESPathExtractor() logger.info('开始执行测试前准备的数据,调用test_Xjx_login方法') self.xjx_login = Test_Xjx_login().test_Xjx_login() self.data['sessionid'] = self.xjx_login logger.debug('测试前准备的数据为:{0}'.format(self.data)) # 获取json配置文件数据 self.jsondata = self.data self.j = JMESPathExtractor() self.get_dbdata = database() self.jsondata['money'] = self.borrow_money self.jsondata['period'] = self.period # def test_test1(self): # print(type(self.jsondata)) def test_xjx_borrowMoney1(self): BorrowMoney = self.URL + self.BorrowMoney_URL logger.info('开始执行app借款接口,caseName:test_xjx_borrowMoney1') self.client = HTTPClient(url=BorrowMoney, method='POST') logger.info('请求的api路径为:{0}'.format(self.BorrowMoney_URL)) logger.info('拼接后的请求路径为:{0}'.format(BorrowMoney)) res = self.client.send(data=self.jsondata) # res = requests.post(url=BorrowMoney, data=self.jsondata) logger.info('返回的参数为:{0}'.format(res.text)) logger.info('接口入参为:query--{0}'.format(self.jsondata)) self.assertIn('中国农业银行', res.text) self.assertIn('成功', res.text) # data.item.card_no_lastFour的值为该测试用户的银行卡最后四位 card_no_lastFour = self.j.extract(query='data.item.card_no_lastFour', body=res.text) # # 判断card_no_lastFour的值是否一致 self.assertEqual(card_no_lastFour, '0002') def test_xjx_borrowMoney2(self): addBorrowMoney = self.URL + self.addBorrowMoney_URL logger.info('开始执行新增app借款接口,caseName:test_xjx_borrowMoney2') self.client = HTTPClient(url=addBorrowMoney, method='POST') logger.info('请求的api路径为:{0}'.format(self.addBorrowMoney_URL)) logger.info('拼接后的请求路径为:{0}'.format(addBorrowMoney)) self.jsondata['speedStatus'] = '' self.jsondata['pay_password'] = '******' res = self.client.send(data=self.jsondata) logger.info('返回的参数为:{0}'.format(res.text)) logger.info('接口入参为:query--{0}'.format(self.jsondata)) self.assertIn('成功', res.text) item_message = self.j.extract(query='data.item.message', body=res.text) self.assertEqual(item_message, '申请成功') order_id = self.j.extract(query='data.item.order_id', body=res.text) logger.info('页面返回的order_id为:{0}'.format(order_id)) select_orderid = "select id from asset_borrow_order where user_id = '768093098' ORDER BY id DESC LIMIT 1" logger.info('查询数据库数据的sql为:{0}'.format(select_orderid)) db_order_id = self.get_dbdata.fetch_one(select_orderid) logger.info('查询数据库的数据,返回的order_id为:{0}'.format(db_order_id))
class Test_Xjx_Dr_BorrowMoney(unittest.TestCase): base_url = Config().get('URL') bm_url = Config().get('before_borrowMoney_url') url = base_url + bm_url logger.info('请求的URL为:{0}'.format(url)) # 调用公共参数 data = JsonConfig().get_jsondata() # test_Xjx_Dr_borrowMoney1调用测试参数 test_data = JsonConfig( path='wholesale', jsonpath='borrowMoney.json').get_jsondata(element='before_borrowMoney') logger.info("测试数据为:{0}".format(test_data)) # test_Xjx_Dr_borrowMoney2调用测试参数 borrowMoney_test_data = JsonConfig( path='wholesale', jsonpath='borrowMoney.json').get_jsondata(element='borrowMoney') logger.info("测试数据为:{0}".format(borrowMoney_test_data)) def setUp(self): self.j = JMESPathExtractor() logger.info('开始执行测试前准备的数据,调用test_Xjx_login方法') self.xjx_login = Test_Xjx_login().test_Xjx_login() self.data['sessionid'] = self.xjx_login logger.info('123测试前准备的数据为:{0}'.format(self.data)) # 获取json测试数据 self.jsondata = self.test_data # 获取headers数据 self.j = JMESPathExtractor() self.test_headers = JsonConfig( path='wholesale', jsonpath='borrowMoney.json').get_jsondata(element='headers') # self.headers = self.j.addextract(query='headers', body=test_headers) self.test_headers['sessionid'] = self.xjx_login logger.info("123测试前准备的headers为:{0}".format(self.test_headers)) self.get_dbdata = database() select_userBankCardId = "SELECT id FROM user_card_info WHERE user_id = '768093098' ORDER BY id DESC LIMIT 1" logger.info('查询数据库数据的sql为:{0}'.format(select_userBankCardId)) db_userBankCardId_id = self.get_dbdata.fetch_one(select_userBankCardId) logger.info('查询数据库的数据,返回的order_id为:{0}'.format(db_userBankCardId_id)) json_str = json.dumps(db_userBankCardId_id) self.one_db_userBankCardId_id = self.j.extract(query='id', body=json_str) logger.info('one_db_userBankCardId_id的数据为:{0}'.format( self.one_db_userBankCardId_id)) self.jsondata['userBankCardId'] = self.one_db_userBankCardId_id logger.info('123data数据为:{0}'.format(self.jsondata)) def test_Xjx_Dr_borrowMoney1(self): ''' 大额借款前置方法(请求江西银行存管查询接口) :return: ''' before_url = self.url # 在测试参数前后追加双引号 str_test_data = "%s" % self.test_data client = HTTPClient(url=before_url, method='POST', headers=self.test_headers) res = client.send(data=str_test_data, params=self.data) logger.info('123client返回的参数:{0}'.format(res.text)) # 获取traceNo参数 self.traceNo = self.j.extract(query='data.traceNo', body=res.text) self.borrowMoney_test_data['traceNo'] = self.traceNo # 获取userBankCardId参数 select_orderid = "select id from asset_borrow_order where user_id = '768093098' ORDER BY id DESC LIMIT 1" logger.info('查询数据库数据的sql为:{0}'.format(select_orderid)) db_order_id = self.get_dbdata.fetch_one(select_orderid) self.userBankCardId = self.j.extract(query='data.traceNo', body=res.text) # 断言 # 获取borrowMoney.json文件中的assertlist数据 self.assertlist = JsonConfig( path='wholesale', jsonpath='borrowMoney.json').get_jsondata( element='before_borrowMoney_assertlist') logger.info('assertlist数据为:{0}'.format(self.assertlist)) # 断言rsq_code rsq_code = self.j.extract(query='code', body=res.text) self.assertEqual(rsq_code, self.j.addextract(query='code', body=self.assertlist)) # 断言rsq_message rsq_message = self.j.extract(query='message', body=res.text) self.assertEqual( rsq_message, self.j.addextract(query='message', body=self.assertlist)) # 断言rsq_orderAmount rsq_orderAmount = self.j.extract(query='data.orderAmount', body=res.text) self.assertEqual( rsq_orderAmount, self.j.addextract(query='orderAmount', body=self.assertlist)) # 断言rsq_bankName rsq_bankName = self.j.extract(query='data.bankName', body=res.text) self.assertEqual( rsq_bankName, self.j.addextract(query='bankName', body=self.assertlist)) # 断言rsq_cardNoLastFour rsq_cardNoLastFour = self.j.extract(query='data.cardNoLastFour', body=res.text) self.assertEqual( rsq_cardNoLastFour, self.j.addextract(query='cardNoLastFour', body=self.assertlist)) def test_Xjx_Dr_borrowMoney2(self): ''' 大额借款方法 :return: ''' borrowMoney_url = Config().get('borrowMoney_url') url = self.base_url + borrowMoney_url logger.info('大额借款的接口地址为:{0}'.format(url)) client = HTTPClient(url=url, method='POST', headers=self.test_headers) #set相关参数到测试参数中 self.borrowMoney_test_data[ 'userBankCardId'] = self.one_db_userBankCardId_id # 在测试参数前后追加双引号 # test_data = self.borrowMoney_test_data.encode("utf-8") str_test_data = "%s" % self.borrowMoney_test_data logger.info('321参数:{0}'.format(str_test_data)) res = client.send(data=str_test_data, params=self.data) logger.info('123456client返回的参数:{0}'.format(res.text))