def test_invest(self, case): #查找参数化的测试数据 print('正则之前的数据', case.param) print(case.url) print(type(case.param)) params = context_1().replace_1(case.param) params = json.loads(params) #这里需要转成字典 res = self.t.requests(case.method, case.url, params) print(res.text) print('正则之后的数据', params) print(type(params)) try: self.assertEqual(case.Expected, res.text) testresult = 'pass' #判断是否加标成功,如果成功就按照借款人的id去数据库查询最新标的记录 if res.json()['msg'] == '加标成功': #admin_id = 1114926 #getattr(context_1,'admin_id ') #sql = "select id FROM future.loan where MemberID = '{0}'/ORDER BY CreateTime DESC LIMIT 1".format(admin_id) sql = "select id FROM future.loan where MemberID = 1114926 ORDER BY CreateTime DESC LIMIT 1" loanId = self.domysql.do_sql(sql)[0] setattr(context_1, 'loanId', str(loanId)) #这里的loanId,需要转化成str,因为正则处理的是字符串 #print('嘿嘿嘿嘿嘿嘿',loanId) except AssertionError as e: testresult = 'faild' finally: doexcel(get_path.cases_path).write_back('invest', case.case_id + 1, res.text, testresult)
def test_login(self, cases): data = json.loads(cases.param) resp = t.requests(cases.method, cases.url, data) try: self.assertEqual(resp.text, cases.Expected) result = 'pass' except AssertionError as e: result = 'faild' raise e finally: doexcel(get_path.cases_path).write_back('login', cases.case_id + 1, resp.text, result)
def test_rechrge(self, cases): print('开始执行第{}条用例'.format(cases.case_id)) data = json.loads(cases.param) res = self.t.requests(cases.method, cases.url, data) # 这里使用self调用 text = json.loads(res.text) try: self.assertEqual(int(text['code']), (cases.Expected)) tesrresult = 'pass' except AssertionError as e: tesrresult = 'faild' raise e finally: doexcel(get_path.cases_path).write_back('recharge', cases.case_id + 1, res.text, tesrresult)
def test_register(self, cases): params = json.loads(cases.param) if params["mobilephone"] == "{}phone": # 判断字典里的手机号是否是"{}phone" params[ "mobilephone"] = self.sql_result + 1 # 重新赋值 #老师 我Excel表里最后三条用例,为什么不通过的 应该是pass才对的 问题已解决 把SQL语句放在setup后 resp = t.requests(cases.method, cases.url, params) try: self.assertEqual(cases.Expected, resp.text) testresult = 'pass' logger.info('第{}条测试用例通过'.format(cases.case_id)) except AssertionError as e: testresult = 'faild' logger.error('第{}条测试用例失败'.format(cases.case_id)) finally: doexcel(get_path.cases_path).write_back('register', cases.case_id + 1, resp.text, testresult)
from common.do_excel import doexcel from common import get_path from conf.conf1 import ConFig import unittest from ddt import ddt, data from common.request import request cases = doexcel(get_path.cases_path).read_data('invest') from common.test_re import context_1 #from common import test_re #不需要再次导入 from common.mysql import domysql ConFig = ConFig() import json @ddt class investTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.t = request() # 实例化对象 这步很重要,可以确保session运行在同一个对象内 cls.domysql = domysql() #cls.sql = ConFig.get_conf('sql', 'sql_2') #domysql.do_sql(cls.sql) def setUp(self): print('.............开始执行测试用例...............') def tearDown(self): print('.............测试用例执行结束...............')
import unittest from ddt import ddt, data from common.do_excel import doexcel from common.request import request from common import get_path import json cases = doexcel(get_path.cases_path).read_data('recharge') # 老师我之前没有把数据和实例化对象都放在类外面,为什么也可以执行成功的 @ddt class rechrge(unittest.TestCase): # cases = doexcel(get_path.cases_path).read_data('recharge') @classmethod # 类方法 def setUpClass(cls): # 针对整个测试类执行的 整个类执行一次 cls.t = request() # 实例化对象 这步很重要,可以确保session运行在同一个对象内 def setUp(self): # 针对测试方法执行的 每个测试用例执行一次 print('.........开始执行测试用例...........') def tearDown(self): print('.........测试用例执行结束...........') @data(*cases) def test_rechrge(self, cases): print('开始执行第{}条用例'.format(cases.case_id)) data = json.loads(cases.param) res = self.t.requests(cases.method, cases.url, data) # 这里使用self调用 text = json.loads(res.text) try:
from common.do_excel import doexcel from common.request import request from common import get_path import json t = request() cases = doexcel(get_path.cases_path).read_data('login') import unittest from ddt import ddt, data @ddt class login(unittest.TestCase): def setUp(self): print('........开始测试.........') print('执行第{}条用例'.format(cases)) @data(*cases) def test_login(self, cases): data = json.loads(cases.param) resp = t.requests(cases.method, cases.url, data) try: self.assertEqual(resp.text, cases.Expected) result = 'pass' except AssertionError as e: result = 'faild' raise e finally: doexcel(get_path.cases_path).write_back('login', cases.case_id + 1, resp.text, result) def tearDown(self):
from common.request import request from common.do_excel import doexcel import unittest from ddt import ddt, data from common import get_path import json t = request() cases = doexcel(get_path.cases_path).read_data('register') # 获取Excel数据 from conf.conf1 import ConFig from libext.ddtnew import ddt, data cof = ConFig().get_conf('sql', 'sql_1') # 获取SQL语句 from common.mysql import domysql from log.mylog import get_logger logger = get_logger(logger_name='python') @ddt class register(unittest.TestCase): def setUp(self): self.sql_result = int(domysql().do_sql( cof)[0]) # 把SQL语句发到setup语句下,就可以替换接下来的手机号 setup是执行一次测试用例,就执行一次 logger.info('..........开始测试............') def tearDown(self): logger.info('..........测试结束............') @data(*cases) def test_register(self, cases): params = json.loads(cases.param)