用VBA在excel表中自动化生成测试用例数据(异常)

最近在做银行接口方面测试,同时也引入了自动化测试执行,自动化执行大家都已实现,但人工维护测试数据繁琐,如某一个字段,字符类型,长度为10,就单这一个字段,异常测试数据就可以达到15条之多,如果一个接口有几十个字段,只维护这类型数据就得花很长时, 基于此,采用根据段长度,类型自动生成测试用例及数据。并把异常测试数据的项自动标红,以便于后期维护。

代码如下:

Sub startMakeTestData()' 用于循环依次生成反向测试用例数据'    initSheet = 5 ' 开始生成的sheet页数    intcount = Sheets.Count ' 工作表的总数        ' --debug 用于调试数据    intcount = 5            For i = initSheet To intcount        Sheets(i).Select         main    NextEnd Sub Sub main()'' 根据一条正向测试用例数据,自动生成反向测试用例数据。主要以长度、类型自动生成反向测试用例数据;'' Author : 刘林' Date   : 2012-5-20'        Dim columnlen ' 列数量    columnlen = getcolumnlen - 1 ' 最后一列为测试执行结果,不使用    Randomize        ' debug  -- 调试多行    ' columnlen = 6          Row = 6 ' 从第几行开始生成数据     sourcerow = 5 ' 源数据行     curRow = 5 ' 当前行     typeFlag = 0 ' 1-数字小数, 2-数字整型, 3-字符     typerow = 4 ' 描述类型的行;     desclen = 3 ' 描述长度的行     intlen = 0 ' 当为小数时,整数部份长度     decLen = 0 ' 当为小数时,小数部份长度            ' --------------- 从第6列 到 最后列,做用例数据生成 ---------    For Column = 6 To columnlen        ' 看是否为 X-字符型,9-数字型,9.99-金额型(保留两位小数)        If IsNumeric(Cells(typerow, Column).Value) Then  ' 步骤 1. 先看是否为数字型            If InStr(Cells(typerow, Column), ".") Then ' 步骤 2. 判定是否为小数 利用 查询小数点作判断                typeFlag = 1 ' 小数                ' 拆分整数部份与小数部份                intlen = Mid(Cells(desclen, Column), 1, InStr(Cells(desclen, Column), ".") - 1)                decLen = Mid(Cells(desclen, Column), InStr(Cells(desclen, Column), ".") + 1)            Else  ' 步骤 3. 前2条件已判断是否为整数,小数,则是整数                typeFlag = 2 ' 整数            End If        Else ' 不是数字,就是字符            typeFlag = 3 ' 字符        End If                ' 根据不同类型生成不同测试数据        If (Int(curRow) > 1) And (Int(curRow) < 65535) Then  ' 防止超出范围出错            Select Case typeFlag                Case 1 ' 1-数字小数                    curRow = makeFloatTestData(curRow, Column, sourcerow, intlen, decLen)                Case 2 ' 2-数字整型  -- 已完成 2012-5-20                    curRow = makeIntTestData(curRow, Column, sourcerow, typeFlag)                Case 3 ' 3-字符                    curRow = makeCharTestData(curRow, Column, sourcerow, typeFlag)            End Select        Else            MsgBox "行值为:" & curRow & ",当前行范围无效,退出行执行!"            Exit Sub ' 当行范围无效时退出        End If        typeFlag = 0 ' 重置为0            Next    ' --------------- 从第6列 到 最后列,做用例数据生成 end ---------    End SubPrivate Function makeIntTestData(curRow, curcolumn, sourcerow, typeFlag)' 生成整数的测试数据' curRow , 当前行值' curcolumn, 当前列' sourcerow, 源行 -- 正向用例列数据' typeFlag,  此列类型标识' 处理整型值,   ' 0. 空   ' 1, 0   ' 2, 小数 (范围内)   ' 3, 长度 -1   ' 4, 长度 +1   ' 5, 负数 (范围内)   ' 6, 长度 +10      Dim arr As String   typearr = Array("空", _                    "零", _                    "小数 (范围内)", _                    "长度 -1", _                    "长度 +1", _                    "负数(范围内)", _                    "长度 +10")                        typelenrow = 3 ' 描述 长度的行    casedscolumn = 4 ' 用例描述的列    typenamerow = 2 ' 字段名 所在行    fieldLen = Int(Cells(typelenrow, curcolumn)) ' 字段长度    ' 1~6种情况处理    For i = 0 To 5        Select Case i        Case 0    ' 空            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = ""            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255        Case 1  ' 0            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = 0            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255        Case 2 ' 小数 (范围内)            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            If Int(Cells(typelenrow, curcolumn).Value) <= 3 Then  ' 如果长度为3位或以下,则直接用1位整数+小数点+ 1位小数                Cells(curRow, curcolumn).Value = makeInt(1) & "." & makeInt(1)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Else ' 否则生成长度一致的小数                Cells(curRow, curcolumn).Value = Mid(String(Int(Cells(typelenrow, curcolumn)), Int((Rnd()) * 9) + 1 & ""), 1, Cells(typelenrow, curcolumn) - 3) & "." & _                                        String(2, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            End If                    Case 3 ' 长度 -1            If Int(Cells(typelenrow, curcolumn)) > 1 Then ' 如果输入长度为 1 则不作 -1 长度测试                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = makeInt(Int(Cells(typelenrow, curcolumn)))                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Else                curRow = curRow - 1 '            End If                    Case 4 ' 长度 +1            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = Mid(String(Int(Cells(typelenrow, curcolumn) + 1), Int((Rnd()) * 9) + 1 & ""), 1, Cells(typelenrow, curcolumn) + 1)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255        Case 5 '  负数 (范围内)            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = "-" & makeInt(fieldLen)                        Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255        Case 6 '  负数 (范围内)            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = "-" & makeInt(fieldLen + 10)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255        End Select                Cells(curRow, 5) = "反"  ' 标识正反用例        Cells(curRow, 2) = Cells(curRow - 1, 2) + 1 ' 标数据增长    Next       makeIntTestData = curRow ' 返回当前行End FunctionFunction copydata(cpsourcerow, cpcurrow)' 从源行拷贝数据到当前行+1'' cpsourcerow -- 拷贝源' cpcurrow    -- 当前行     Rows(cpsourcerow).Select     Selection.Copy     Rows(cpcurrow + 1).Select     ActiveSheet.Paste     cpcurrow = cpcurrow + 1     copydata = cpcurrowEnd FunctionFunction getcolumnlen()' 取第一行的长度,'    For i = 1 To 256        If Cells(1, i).Value = "" Then            getcolumnlen = i - 1            Exit Function        End If    NextEnd FunctionPrivate Function makeFloatTestData(curRow, curcolumn, sourcerow, mftdintlen, mftddecLen)' 生成小数测试数据' curRow , 当前行值' curcolumn, 当前列' sourcerow, 源行 -- 正向用例列数据' typeFlag,  此列类型标识' mftdintlen 整数部份长度' mftddecLen 小数部份长度' 处理带小数值情况,    ' --    0   空    ' --    1   零    ' --    2   完全整数,为指定 长度    ' --    3   整数部份超 +1 长度,小数部份为指定长度    ' --    4   整数据部份为指定长度,小数部份超 +1 长度    ' --    5   整数部份超 +1 长度,小数部份超 +1 长度    ' --    6   负数(整数指定长度范围内)    ' --    7   负数(整数部份超 +1 长度,小数部份为指定长度   )    ' --    8   负数(整数据部份为指定长度,小数部份超 +1 长度)    ' --    9   负数(整数部份超 +1 长度,小数部份超 +1 长度  )    ' --    10  指定长度范围内,整数部份有特殊字符    ' --    11  指定长度范围内,小数部份有特殊字符    ' --    12  整数部份超 -1 长度,小数部份为指定长度    ' --    15  负数(整数部份超 -1 长度,小数部份为指定长度   )    ' --    17  负数(整数部份超 -1 长度,小数部份超 -1 长度  )    ' --    13  整数据部份为指定长度,小数部份超 -1 长度    ' --    14  整数部份超 -1 长度,小数部份超 -1 长度    ' --    16  负数(整数据部份为指定长度,小数部份超 -1 长度)        Dim arr As String    typearr = Array("空", _                    "零", _                    "只有整数部分,长度为整数与小数长度之合", _                    "整数部份超 +1 长度,小数部份为指定长度", _                    "整数据部份为指定长度,小数部份超 +1 长度", _                    "整数部份超 +1 长度,小数部份超 +1 长度", _                    "负数(整数指定长度范围内)", _                    "负数(整数部份超 +1 长度,小数部份为指定长度)", _                    "负数(整数据部份为指定长度,小数部份超 +1 长度)", _                    "负数(整数部份超 +1 长度,小数部份超 +1 长度)", _                    "指定长度范围内,整数部份有特殊字符", _                    "指定长度范围内,小数部份有特殊字符", _                    "整数部份超 -1 长度,小数部份为指定长度", _                    "负数(整数部份超 -1 长度,小数部份为指定长度)", _                    "负数(整数部份超 -1 长度,小数部份超 -1 长度)", _                    "整数据部份为指定长度,小数部份超 -1 长度", _                    "整数部份超 -1 长度,小数部份超 -1 长度", _                    "负数(整数据部份为指定长度,小数部份超 -1 长度)")                        typelenrow = 3 ' 描述 长度的行    casedscolumn = 4 ' 用例描述的列    typenamerow = 2 ' 字段名 所在行    testTypeLen = UBound(typearr) - LBound(typearr) - 1 ' 检测类型 组合长度            If (mftdintlen < 1) Or (mftddecLen < 1) Then        MsgBox "字段长度值非法,请检查,行为:" & typelenrow & "列为:" & curcolumn        Exit Function    End If    For i = 0 To testTypeLen        Select Case i            Case 0      ' --    空                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                                ' --- 生成数据                Cells(curRow, curcolumn).Value = ""                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 1      ' --    零                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                ' --- 生成数据                Cells(curRow, curcolumn).Value = 0                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 2      ' --    完全整数,为指定 长度                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = String(Int(mftdintlen) + Int(mftddecLen), Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 3      ' --    整数部份超 +1 长度,小数部份为指定长度                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 4      ' --    整数据部份为指定长度,小数部份超 +1 长度                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = String(mftdintlen, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 5      ' --    整数部份超 +1 长度,小数部份超 +1 长度                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 6      ' --    负数(整数指定长度范围内)                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 7      ' --    负数(整数部份超 +1 长度,小数部份为指定长度   )                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 8      ' --    负数(整数据部份为指定长度,小数部份超 +1 长度)                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                            Case 9      ' --    负数(整数部份超 +1 长度,小数部份超 +1 长度  )                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = "-" & String(mftdintlen + 1, Int((Rnd()) * 9) + 1 & "") & "." & String(mftddecLen + 1, Int((Rnd()) * 9) + 1 & "")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 10     ' --    指定长度范围内,整数部份有特殊字符                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = makeInt(mftdintlen - 1) & makeSpecialChar(1) & "." & makeInt(mftddecLen)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 11     ' --    指定长度范围内,小数部份有特殊字符                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = makeInt(mftdintlen) & "." & makeInt(mftddecLen - 1) & makeSpecialChar(1)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 12     ' --    整数部份超 -1 长度,小数部份为指定长度                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 15     ' --    负数(整数部份超 -1 长度,小数部份为指定长度   )                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = "-" & makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 17     ' --    负数(整数部份超 -1 长度,小数部份超 -1 长度  )                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = "-" & makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen - 1)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 13     ' --    整数据部份为指定长度,小数部份超 -1 长度                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = makeInt(mftdintlen) & "." & makeInt(mftddecLen - 1)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 14     ' --    整数部份超 -1 长度,小数部份超 -1 长度                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = makeInt(mftdintlen - 1) & "." & makeInt(mftddecLen - 1)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255            Case 16     ' --    负数(整数据部份为指定长度,小数部份超 -1 长度)                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = "-" & makeInt(mftdintlen) & "." & makeInt(mftddecLen - 1)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255        End Select                Cells(curRow, 5) = "反"  ' 标识正反用例        Cells(curRow, 2) = Cells(curRow - 1, 2) + 1 ' 标数据增长    Next       makeFloatTestData = curRow ' 返回当前行End FunctionPrivate Function makeCharTestData(curRow, curcolumn, sourcerow, typeFlag)' 生成 字符 型 测试数据' curRow , 当前行值' curcolumn, 当前列' sourcerow, 源行 -- 正向用例列数据' typeFlag,  此列类型标识' mftdintlen 整数部份长度' mftddecLen 小数部份长度' 处理字符 型值情况,    ' --0   空    ' --1   正常长度,空白    ' --2   正常长度,数字    ' --3   正常长度, 字母    ' --4   正常长度, 特殊符号(含中文)    ' --5   正常长度, 数字、特殊符号(含中文)、字母混合    ' --6   超长度 + 1, 空白    ' --7   超长度 + 1, 数字    ' --8   超长度 + 1, 字母    ' --9   超长度 + 1, 特殊符号    ' --10  超长度 + 1, 数字、特殊符号(含中文)、字母    ' --11  短长度 - 1, 空白    ' --12  短长度 - 1, 数字    ' --13  短长度 - 1, 字母    ' --14  短长度 - 1, 特殊符号    ' --15  短长度 - 1, 数字、特殊符号(含中文)、字母    Dim arr As String    typearr = Array("空", "正常长度,空白", "正常长度,数字", "正常长度, 字母", "正常长度, 特殊符号", _                    "正常长度, 数字、特殊符号(含中文)、字母混合", "超长度 + 1, 空白", "超长度 + 1, 数字", _                    "超长度 + 1, 字母", "超长度 + 1, 特殊符号", "超长度 + 1, 数字、特殊符号(含中文)、字母", _                    "短长度 - 1, 空白", "短长度 - 1, 数字", "短长度 - 1, 字母", "短长度 - 1, 特殊符号", _                    "短长度 - 1, 数字、特殊符号(含中文)、字母")                        typelenrow = 3 ' 描述 长度的行    casedscolumn = 4 ' 用例描述的列    typenamerow = 2 ' 字段名 所在行    fieldLen = Int(Cells(typelenrow, curcolumn)) ' 字段长度    testTypeLen = UBound(typearr) - LBound(typearr) - 1 ' 检测类型 组合长度        ' debug 长度值调试    If fieldLen < 1 Then        MsgBox "字段长度值非法,请检查,行为:" & typelenrow & "列为:" & curcolumn        Exit Function    ElseIf fieldLen = 1 Then        testTypeLen = testTypeLen - 5   ' 输入长度为1,则只长度-1测试可以不做    End If        For i = 0 To testTypeLen        Select Case i        Case 0      ' --    0   空                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                ' --- 生成数据                Cells(curRow, curcolumn).Value = ""                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                        Case 1      ' --    1   正常长度,   空白                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                Cells(curRow, curcolumn).Value = String(fieldLen, " ")                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                rowaddFlag = True                        Case 2      ' --    2   正常长度,   数字                curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                ' --- 生成数据                Cells(curRow, curcolumn).Value = makeInt(fieldLen)                Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"                Cells(curRow, curcolumn).Interior.Color = 255                        Case 3      ' --    3   正常长度,   字母            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                ' --- 生成数据            Cells(curRow, curcolumn).Value = makeLCase(fieldLen)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                        Case 4      ' --    4   正常长度,   特殊符号            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                ' --- 生成数据            Cells(curRow, curcolumn).Value = makeSpecialChar(fieldLen)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 5      ' --    5   正常长度,   数字、特殊符号(含中文)、字母混合            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                        ' --- 生成数据            ' 如果长度低于3位时,需处理            If (fieldLen = 1) Then                Cells(curRow, curcolumn).Value = makeSpecialChar(1)            ElseIf (fieldLen = 2) Then                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1)            ElseIf (fieldLen = 3) Then                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeSpecialChar(1)            Else ' 大于3位时,全部混合, 字母+ 数字 + 中文+ 特殊 (其余位)                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) & makeSpecialChar(fieldLen - 4)            End If            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 6      ' --    6   超长度 + 1,     空白            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = String(fieldLen + 1, " ")            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                        Case 7      ' --    7   超长度 + 1,     数字            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = makeInt(fieldLen + 1)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 8      ' --    8   超长度 + 1,     字母            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = makeLCase(fieldLen + 1)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 9      ' --    9   超长度 + 1,     特殊符号            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = makeSpecialChar(fieldLen + 1)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 10     ' --    10  超长度 + 1,     数字、特殊符号(含中文)、字母            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                        ' --- 生成数据            ' 如果长度低于2位时,需处理            If (fieldLen = 1) Then                 Cells(curRow, curcolumn).Value = makeSpecialChar(1)            ElseIf (fieldLen = 2) Then                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeSpecialChar(1)            ElseIf (fieldLen = 3) Then                Cells(curRow, curcolumn).Value = makeLCase(1) & makeSpecialChar(1) & makeChinaChar(2)            ElseIf (field = 4) Then  ' 等于4位时,全部混合, 字母+ 数字 + 中文(2)                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) _                                                & makeLCase(1) ' 多加一位            ElseIf (field > 4) Then  ' 大于4位时,全部混合, 字母+ 数字 + 中文+ 特殊 (其余位)                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) & makeSpecialChar(fieldLen - 4) _                                                & makeLCase(1) ' 多加一位            End If            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                                Case 11     ' --    11  短长度 - 1,     空白            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = String(fieldLen - 1, " ")            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                        Case 12     ' --    12  短长度 - 1,     数字            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = makeInt(fieldLen - 1)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 13     ' --    13  短长度 - 1,     字母            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = makeLCase(fieldLen - 1)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 14     ' --    14  短长度 - 1,     特殊符号            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据            Cells(curRow, curcolumn).Value = makeSpecialChar(fieldLen - 1)            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    Case 15     ' --    15  短长度 - 1,     数字、特殊符号(含中文)、字母            curRow = copydata(sourcerow, curRow) ' --- 从源拷贝数据                        ' --- 生成数据            ' 如果长度低于2位时,需处理            If (fieldLen = 1) Then                Rows(curRow).Select                ActiveSheet.Delete                curRow = curRow - 1 ' 当长度要求为1时,少一位就相当于空,所以回滚一行            ElseIf (fieldLen = 2) Then                Cells(curRow, curcolumn).Value = makeSpecialChar(1)            ElseIf (fieldLen = 3) Then                Cells(curRow, curcolumn).Value = makeSpecialChar(1) & makeInt(1)            ElseIf (fieldLen = 4) Then ' 等于4位时,全部混合, 字母+ 中文                Cells(curRow, curcolumn).Value = makeLCase(1) & makeChinaChar(2)            ElseIf (fieldLen = 5) Then ' 等于 5 位时,全部混合, 字母+ 数字 + 中文                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2)            ElseIf (fieldLen > 5) Then ' 大于 5 位时,全部混合, 字母+ 数字 + 中文+ 特殊 (其余位)                Cells(curRow, curcolumn).Value = makeLCase(1) & makeInt(1) & makeChinaChar(2) & makeSpecialChar(fieldLen - 5)            End If            Cells(curRow, casedscolumn).Value = Cells(typenamerow, curcolumn) & " 值为:" & typearr(i) & ", 交易失败"            Cells(curRow, curcolumn).Interior.Color = 255                    End Select                Cells(curRow, 5) = "反"  ' 标识正反用例        Cells(curRow, 2) = Cells(curRow - 1, 2) + 1 ' 标数据增长    Next       makeCharTestData = curRow ' 返回当前行End FunctionFunction makeInt(makeIntlen)' 生成指定长度的数字' strmakeIntlen, 需要生成的长度    ' debug    'strmakeIntlen = 9    If makeIntlen < 0 Then        MsgBox "makeIntlen 参数小于0 ,退出!"        Exit Function    End If        Dim strintdata    strintdata = ""    For i = 1 To makeIntlen        strintdata = strintdata & Chr(Int((Rnd() * 10 + 48)))    Next            makeInt = strintdataEnd FunctionFunction makeLCase(makeLCaselen)' 生成指定长度的数字' makeLCaselen, 需要生成的长度    ' debug    'strmakeIntlen = 9    If makeLCaselen < 0 Then        MsgBox "makeLCaselen 参数小于0 ,退出!"        Exit Function    End If        Dim strintdata    strintdata = ""    For i = 1 To makeLCaselen        strintdata = strintdata & Chr(Int((Rnd() * 26 + 97)))    Next            makeLCase = strintdataEnd FunctionFunction makeSpecialChar(makeSpecialCharlen)' 生成  特殊字符' makeSpecialCharlen, 需要生成的长度    ' debug    'strmakeIntlen = 9    If makeSpecialCharlen < 0 Then        MsgBox "makeSpecialCharlen 参数小于0 ,退出!"        Exit Function    End If            '    Dim strintdata    strintdata = ""    For i = 1 To makeSpecialCharlen        ' 因特殊符号,不在相邻的ascii码值内,分为4个阶段        If ((10 Mod 4) = 0) Then            strintdata = strintdata & Chr(Int((Rnd() * 33 + 15)))        ElseIf ((10 Mod 4) = 1) Then            strintdata = strintdata & Chr(Int((Rnd() * 7 + 58)))        ElseIf ((10 Mod 4) = 2) Then            strintdata = strintdata & Chr(Int((Rnd() * 6 + 91)))        ElseIf ((10 Mod 4) = 3) Then            strintdata = strintdata & Chr(Int((Rnd() * 4 + 123)))        End If    Next        makeSpecialChar = strintdataEnd FunctionFunction makeChinaChar(makeChinaCharlen)' 生成  特殊字符' makeSpecialCharlen, 需要生成的长度' 一个中文占2个    ' debug    'strmakeIntlen = 9    If makeChinaCharlen < 0 Then        MsgBox "makeChinaCharlen 参数小于0 ,退出!"        Exit Function    End If        ' arry  长度为 19, 类型为中文    Dim arry    arry = Array("使", "用", "百", "度", "特", "殊", "符", _                "号", "中", "文", "以", "第", "一", "时", _                "间", "收", "到", "提", "问", "有", "新", _                "回", "答", "回", "答", "被", "采", "纳", _                "网", "友", "求", "助", "的", "通", "查", _                "看", "详", "情")    Dim strintdata    strintdata = ""        ' 当长度为奇数时,最后一位用字母    If (makeChinaCharlen Mod 2) = 1 Then        makeChinaCharlen = makeChinaCharlen - 1        For i = 1 To makeChinaCharlen Step 2            strintdata = strintdata & arry(Rnd * 19 + 1)        Next        ' 为奇数时,最后付一位字母        strintdata = strintdata & Chr(Int((Rnd() * 26 + 97)))    Else        For i = 1 To makeChinaCharlen Step 2            strintdata = strintdata & arry(Rnd * 19 + 1)        Next    End If            makeChinaChar = strintdataEnd Function

会让你的心态更平和更坦然,也会让你心无旁骛,更会让你的心灵得到解脱和抚慰。

用VBA在excel表中自动化生成测试用例数据(异常)

相关文章:

你感兴趣的文章:

标签云: