Soapui接口功能测试参数化

2017-06-02  本文已影响0人  御剑逍遥
image.png

1.Soapui的常用参数化类型

image.png

2.Soapui参数化操作方式

菜单路径:Test Steps->Add Step(右键菜单)->Data Source


image.png

3.Txt 文件参数化

image.png

数据之间要用逗号做分隔符,否则soapui没办法区分数据


image.png

1.增加参数
2.选择数据类型
3.选择数据文件
4.数据的分隔符


image.png

4.Excel文件参数化

image.png image.png

1.增加参数
2.选择数据类型
3.选择数据文件
4.起始单元格


image.png

5.Groovy脚本参数化

groovy是基于java语言的脚本语 言,语法结构与java雷同,或者直接写java代码也是可以的

要求:姓名,手机号码,email不能重复

1.随机生成姓名的代码
public List<String> RandomUserName(int RandomUserName) {
    String[] FirstName = ["赵","钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
        "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
        "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷"];
    List<String> randomName = new ArrayList<>();
    for (int i = 1; i <= RandomUserName; i++) {
        Random randomFirstName = new Random();
        int index = randomFirstName.nextInt(FirstName.length - 1);
        String name = FirstName[index];
        if(randomFirstName.nextBoolean()){
            name += getChinese()+getChinese();
            randomName.add(name);
        }
        else{
            name += getChinese();
            randomName.add(name);
        }
    }
    return randomName;
}
public static String getChinese() {
    String str = null;
    int highPos, lowPos;
    Random randomChinese = new Random();
    highPos = (176 + Math.abs(randomChinese.nextInt(55)))
    randomChinese = new Random();
    lowPos = 161 + Math.abs(randomChinese.nextInt(94))

    byte[] bArr = new byte[2];
    bArr[0] = (new Integer(highPos)).byteValue();
    bArr[1] = (new Integer(lowPos)).byteValue();
    try {
        str = new String(bArr, "GB2312");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return str;
}
2.随机生成手机号的代码
public List<String> RandomTelNum(int RandomTel) {
    List<String> RandomTelNum = new ArrayList<>();
    for (int i = 0; i < RandomTel; i++) {
        String TelNumHead = "170";
        int min = 10000000;
        int max = 99999999;
        Random randomTelLast = new Random();
        int s = randomTelLast.nextInt(max) % (max - min + 1) + min;
        String telNum = TelNumHead + Integer.toString(s);
        RandomTelNum.add(telNum);
    }
    return RandomTelNum;
}
3.随机生成Email的代码
public List<String> RandomEmail(int RandomEmail) {
    List<String> randomEmail = new ArrayList<>();
    for (int i = 0; i < RandomEmail; i++) {
        String[]  randomName = "ab,bc,cd,de,ef,fg,gh,hi,ij,jk,kl,lm,mn,no,op,pq,qr,rs,st,tu,uv,vw,wx,xy,yz,za".split(",");
        Random randomletter = new Random();
        int index = randomletter.nextInt(26);
        String EmailName = randomName[index] +randomName[index/3]+ System.currentTimeMillis();
        EmailName += getemail_suffix();
        randomEmail.add(EmailName);
    }
    return randomEmail;
}
public static String getemail_suffix() {
    String[] email_suffix = "@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(",");
    String str;
    Random randomEmail_suffix = new Random();
    int index = randomEmail_suffix.nextInt(email_suffix.length - 1);
    str = email_suffix[index];
    return str;
}
image.png
//随机生成姓名
println(RandomUserName(1)[0]);
//代码产生的数据与变量关联
result["name"]=RandomUserName(1)[0]
//随机生成邮箱
println(RandomEmail(1)[0]);
//代码产生的数据与变量关联
result["email"]=RandomEmail(1)[0]
//随机生成手机号码
println(RandomTelNum(1)[0]);
//代码产生的数据与变量关联
result["mobile"]=RandomTelNum(1)[0]

6.JDBC参数化

当接口产生的数据需要从数据库读取的时候我们可以连接数据对数据查询进行数据关联,soapui是可以链接多种数据库,我们使用mysql做演示

1.下载mysql驱动的jar包
http://pan.baidu.com/s/1ge5BLhL
2.拷贝驱动jar到如下路径(soapui的安装路径)
C:\Program Files\SmartBear\SoapUI-Pro-5.1.2\jre\lib\ext

数据库连接成功,说明数据源已经配置好


image.png

select userName,passWord from user

image.png

7.参数关联到Request

这一步虽然比较简单,但是至关重要,否则参数化是体现不出来的


image.png
自此Soapui常用参数化过程就剖析到这里,下一篇我会带着大家一起看requet间,testCase间的数据传递,欢迎伙伴们持续关注,谢谢。
上一篇下一篇

猜你喜欢

热点阅读