def set_result(testResult,assertMsg):
    '''
    设置测试结果,如果不是PASS将结束执行。
    :param testResult: 测试结果
    :param assertMsg: 断言信息
    :return: 无返回
    '''
    #testResult必须是PASS FAIL ERROR
    resList = ["PASS","FAIL","ERROR"]
    if testResult not in resList:
        context.setERROR( "set_result的第一个参必须是%s中的一个" % resList)
        raise MyAssertError("ERROR", "set_result的第一个参必须是%s中的一个" % resList)
    else:
        context.setResult(testResult,assertMsg)
        if testResult == "FAIL" or testResult == "ERROR":
            raise MyAssertError(testResult,assertMsg)
Esempio n. 2
0
def assert_struct(expectJson, actual=None):
    '''
    断言数据结构
    :param expectJson: 预期结构,必填
    :param actual: 实际结构,选填,如果不写默认使用常量的返回值
    :return:
    '''
    try:
        #开始处理断言语句
        retStr = ""
        standardJson = expectJson.strip()
        if actual:
            actualJson = actual
        else:
            if context.protocol == "HTTP":
                #如果不是dubbo就是http的
                actualJson = getRespTextByResponse(context.interface_response)
            elif context.protocol == "DUBBO":
                #如果是dubbo,就用dubbo的actualResult
                actualJson = context.actualResult
            else:
                context.testResult = "ERROR"
                retStr = "错误的协议类型:%s" % context.protocol
                context.assertResult += retStr + "\n"
                return retStr

        if isJson(standardJson) == False:
            context.testResult = "ERROR"
            retStr = "<ERROR:assert_struct执行时预期结构不是json>"
            context.assertResult += retStr + "\n"
            return retStr
        if isJson(actualJson) == False:
            context.testResult = "FAIL"
            retStr = "FAIL: assert_struct执行时返回的内容不是JSON。"
            context.assertResult += retStr + "\n"
            return retStr

        # 开始进行结构断言
        retList = Assert.assertStruct(standardJson, actualJson)
        retStr = retList[1].strip() + "\n\n"
        context.testResult = retList[0]
        context.assertResult += retStr
        return retStr

    except:
        log(traceback.format_exc())
        retStr = traceback.format_exc()
        context.testResult = "ERROR"
        context.assertResult += retStr
    finally:
        retResList = retStr.strip().split("\n")
        log("断言结束,断言结果:%s" %
            retResList[0] if len(retResList) == 1 else retResList[0] +
            "...(超出部分详见断言结果)")
        if context.testResult in context.exitExecStatusList:
            raise MyAssertError(context.testResult, retStr)
        return retStr
def asserts(assertStr,actual = None):
    '''
    断言函数
    :param assertStr:可以是一个非json,进行包含断言,可以是json,进行递归断言,可以是bool类型的,直接断言。也可以调用平台的[==]等进行断言
    :return: 一些输入,断言结果会直接写入context的testResult属性
    '''
    try:
        retStr = ""
        #如果是bool类型的,直接返回通过失败。
        if isinstance(assertStr,bool):
            if assertStr:
                context.testResult = "PASS"
                retStr = "断言结果是True,断言通过!"
                context.assertResult += retStr+"\n"
                return retStr
            else:
                context.testResult = "FAIL"
                retStr = "断言结果是False,断言失败!"
                context.assertResult += retStr + "\n"
                return retStr

        #如果是字符串类型的,开始常规断言
        if not isinstance(assertStr,str) and not isinstance(assertStr,int) and not isinstance(assertStr,float):
            context.testResult = "ERROR"
            retStr = "<ERROR: %s 必须是str/int/float/bool类型!>" % str(assertStr)
            context.assertResult += retStr + "\n"
            return retStr

        #开始处理断言语热天句
        tmpAssertString = str(assertStr).strip()
        if tmpAssertString == "":
            context.testResult = "ERROR"
            retStr = "<ERROR: 断言的string不能是空字符串!>"
            context.assertResult += retStr + "\n"
            return retStr
        else:
            tmpAssertString = core.processor.KP.KP.getProcessedValue(tmpAssertString, context)
            if actual is None:
                if context.protocol == "HTTP":
                    #如果不是dubbo就是http的
                    assertText = getRespTextByResponse(context.interface_response)
                elif context.protocol=="DUBBO":
                    #如果是dubbo,就用dubbo的actualResult
                    assertText = context.actualResult
                else:
                    context.testResult = "ERROR"
                    retStr = "错误的协议类型:%s" % context.protocol
                    context.assertResult += retStr + "\n"
                    return retStr
            else:
                assertText = actual

            retList = Assert.assertExpectText(tmpAssertString, assertText)
            retStr = retList[1].strip() + "\n\n"
            context.testResult = retList[0]
            context.assertResult += retStr
            return retStr
    except:
        log(traceback.format_exc())
        retStr = traceback.format_exc()
        context.testResult = "ERROR"
        context.assertResult += retStr
    finally:
        retResList = retStr.strip().split("\n")
        log("断言结束,断言结果:%s" % retResList[0] if len(retResList) == 1 else retResList[0]+"...(超出部分详见断言结果)")
        if context.testResult in context.exitExecStatusList:
            raise MyAssertError(context.testResult,retStr)
        return retStr