def writeTestResultToDb(testResult=None, title=u"Steam测试报告", description=u"用例测试情况"): DbManager.cleanDB() dbManager = DbManager( host= "steam-uat-default.crbcfaeazoqe.rds.cn-northwest-1.amazonaws.com.cn", user="******", password="******", dbname="ltjtest", port=3306) result_list = testResult.result nowdatestr = getNowTime() plandict = { "plantime": nowdatestr, "projectname": title, "description": description } starttime = sys.argv[1] processSql = "update test_run_process set status=2,endtime = '%s' where projectname = '%s' and starttime = '%s';" % ( nowdatestr, title, starttime) dbManager.updateData(processSql) plansqlStr = "insert into test_plan(plantime,projectname,description) values('%(plantime)s','%(projectname)s','%(description)s'); " dbManager.insertData(plansqlStr % plandict) planidStr = "select max(id) id from test_plan;" idrst = dbManager.queryAll(sql=planidStr) id = idrst[0][0] # n=异常,错误,成功, # t = 测试用例对象 TestCase # o = , # e = 异常信息 for n, t, o, e in result_list: caseResultDic = {} caseResultDic['result_sign'] = n caseResultDic['plan_id'] = id caseResultDic['classname'] = t.__class__ caseResultDic['interfacename'] = t.__interfaceName__ caseResultDic['testcaseid'] = t.getCaseid() caseResultDic['testpoint'] = t.getTestPoint() if isinstance(o, str): # TODO: some problem with 'string_escape': it escape \n and mess up formating # uo = unicode(o.encode('string_escape')) # uo = o.decode('latin-1') uo = e else: uo = o if isinstance(e, str): # TODO: some problem with 'string_escape': it escape \n and mess up formating # ue = unicode(e.encode('string_escape')) # ue = e.decode('latin-1') ue = e else: ue = e script = "%(output)s" % dict(output=saxutils.escape(uo + ue), ) caseResultDic['errordes'] = dbManager.conn.escape(script) sqlstr = "insert into test_case_record(classname,interfacename,testcaseid,testpoint,plan_id,result_sign,errordes) values(\"%(classname)s\" , '%(interfacename)s','%(testcaseid)s','%(testpoint)s','%(plan_id)s','%(result_sign)s',\"%(errordes)s\")" insertSql = sqlstr % caseResultDic dbManager.insertData(insertSql)
def getRunTestTokenId(projectname="", starTime="sss"): dbManager = getDbManger() starttime = getNowTime() tokenId = uuid.uuid4() sqlstr = "insert into test_run_process(token,starttime,status,projectname) value('%s','%s',1,'%s')" % ( tokenId, starttime, projectname) dbManager.insertData(sql=sqlstr, dbName="ltjtest") return tokenId, starttime
def writeTestResultToDb(testResult=None, title=u"Steam测试报告", description=u"用例测试情况", token="sss"): dbManager = getDbManger() result_list = testResult.result nowdatestr = getNowTime() plandict = { "plantime": nowdatestr, "projectname": title, "description": description } processSql = "update test_run_process set status=2,endtime = '%s' where projectname = '%s' and token = '%s';" % ( nowdatestr, title, token) print("proccesSql = %s" % processSql) # plansqlStr = "insert into test_plan(plantime,projectname,description) values('%(plantime)s','%(projectname)s','%(description)s') ; " % plandict # print("plansqlStr = %s" % plansqlStr) # # dbManager.insertData(sql=plansqlStr, dbName="ltjtest") planidStr = "select max(id) id from test_plan;" idrst = dbManager.queryAll(sql=planidStr, dbName="ltjtest") id = idrst[0][0] # 更新planId到test_run_process表 updateProceeSql = "update test_run_process p set p.planId = %d where p.token = '%s';" % ( id, token) dbManager.updateData(sql=updateProceeSql, dbName="ltjtest") # n = 异常,错误,成功, # t = 测试用例对象 TestCase # o = , #e = 异常信息 for n, t, o, e in result_list: caseResultDic = {} caseResultDic['result_sign'] = n caseResultDic['plan_id'] = id caseResultDic['classname'] = t.__class__ caseResultDic['interfacename'] = "https://uat-steam-api.opg.cn" + \ t.__interfaceName__ caseResultDic['testcaseid'] = t.getCaseid() caseResultDic['testpoint'] = t.getTestPoint() if isinstance(o, str): # TODO: some problem with 'string_escape': it escape \n and mess up formating # uo = unicode(o.encode('string_escape')) # uo = o.decode('latin-1') uo = e else: uo = o if isinstance(e, str): # TODO: some problem with 'string_escape': it escape \n and mess up formating # ue = unicode(e.encode('string_escape')) # ue = e.decode('latin-1') ue = e else: ue = e script = "%(output)s" % dict(output=saxutils.escape(uo + ue), ) # caseResultDic['errordes'] = dbManager.conn.escape(script) caseResultDic['errordes'] = pymysql.escape_string(script) sqlstr = "insert into test_case_record(classname,interfacename,testcaseid,testpoint,plan_id,result_sign,errordes) values(\"%(classname)s\" , '%(interfacename)s','%(testcaseid)s','%(testpoint)s','%(plan_id)s','%(result_sign)s',\"%(errordes)s\")" insertSql = sqlstr % caseResultDic dbManager.insertData(sql=insertSql, dbName="ltjtest") dbManager.updateData(sql=processSql, dbName="ltjtest")