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
Beispiel #2
0
def assert_struct(expectJson, actual=None):
    '''
    断言数据结构
    :param expectJson: 预期结构,必填
    :param actual: 实际结构,选填,如果不写默认使用常量的返回值
    :return:
    '''
    try:
        #开始处理断言语句
        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:
                actualJson = "错误的协议类型:%s" % context.protocol
                set_result("ERROR", actualJson)
                return actualJson

        if isJson(standardJson) == False:
            retMsg = "<ERROR:assert_struct执行时预期结构不是json>"
            set_result("ERROR", retMsg)
            return retMsg
        if isJson(actualJson) == False:
            retMsg = "<ERROR:assert_struct执行时实际结构不是json>"
            set_result("ERROR", retMsg)
            return retMsg

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

    except:
        retStr = traceback.format_exc()
        set_result("ERROR", retStr)
        return retStr