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

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

代码如下:

 

Sub startMakeTestData()
' 用于循环依次生成反向测试用例数据

'
    initSheet = 5 ' 开始生成的sheet页数
    intcount = Sheets.Count ' 工作表的总数
    
    ' --debug 用于调试数据
    intcount = 5
        
    For i = initSheet To intcount
        Sheets(i).Select
         main
    Next
End 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 Sub
Private 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) 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值