赞
踩
按照自动化测试分层实现的原理,每一层的脚本实现都要进行参数化,自动化的目标就是要实现脚本代码与测试数据分离。
当测试数据进行调整的时候不会对脚本的实现带来震荡,从而提高脚本的稳定性与灵活度,降低脚本的维护成本。
Soapui最为强大的参数化就是支持groovy脚本的参数化或者java代码,任何无法实现的地方都可以考虑脚本的扩展。
菜单路径:Test Steps->Add Step(右键菜单)->Data Source
数据之间要用逗号做分隔符,否则soapui没办法区分数据
1.增加参数
2.选择数据类型
3.选择数据文件
4.数据的分隔符
image.png
1.增加参数
2.选择数据类型
3.选择数据文件
4.起始单元格
groovy是基于java语言的脚本语 言,语法结构与java雷同,或者直接写java代码也是可以的
要求:姓名,手机号码,email不能重复
- 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;
- }
- 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;
- }
-
运行的效果
- //随机生成姓名
- 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]
当接口产生的数据需要从数据库读取的时候我们可以连接数据对数据查询进行数据关联,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
配置数据源
数据库连接成功,说明数据源已经配置好
select userName,passWord from user
这一步虽然比较简单,但是至关重要,否则参数化是体现不出来的
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。