testng接口自动化测试_接口自动化测试平台系列:场景化执行

b71769ea298533d5071d115169d66fee.gif

e42c8644fc594aef35011edd93d00e67.png

关注测试君 | 会上瘾

142bebd506192049eefdc5f40b566198.gif

前端很简单,点击“执行”按钮即可,会像后端传入该集合的id

fe6bdec55c8b94296b11d859a9c9ded6.png

后端拿到集合id后,会执行以下几步操作:

  • 拿到所有全局变量

  • 拿到集合变量(集合变量只会在该集合内生效,不会在其他集合中生效)

  • 声明测试报告的保存路径

  • 更新集合的最后执行人与最后执行时间

  • 调用testng类,进行接口执行,并生成报告

Service

@Overridepublic ResponseVo collectionExcute(Integer collectionId) {
        ResponseVo responseVo = new ResponseVo();
        ApiTestConfig.apiList = apiTestCaseMapper.selectCollectionApiList(collectionId);
        ApiTestConfig.collectionId = collectionId;// 全局变量赋值
        List globalVariableList = apiTestConfigMapper.selectGlobalVariable();for (Variable variable:globalVariableList){
            ApiTestConfig.globalVariableMap.put(variable.getVariableName(),variable.getVariableValue());
        }// 集合变量赋值
        List collectionVariableList = apiTestCollectionMapper.selectCollectionVariable(collectionId);for (Variable variable:collectionVariableList){
            ApiTestConfig.collectionVariableMap.put(variable.getVariableName(),variable.getVariableValue());
        }// 测试报告保存路径
        ApiTestConfig.reportName = String.valueOf(System.currentTimeMillis())+ ".html";
        Collection collection = new Collection();
        collection.setId(collectionId);
        collection.setReportPath("/report/" + ApiTestConfig.reportName);// 更新最后执行人和最后执行时间
        User u = HttpBasicAuthorizeAttribute.getTokenUserInfo().get();
        collection.setExcuterCode(u.getCode());
        collection.setExcuterName(u.getName());
        collection.setExcuteDatetime(new Date());
        apiTestCollectionMapper.updateCollection(collection);// 测试执行
        TestNG testNg = new TestNG();
        Class[] listenerClass = {ExtentTestNGIReporterListener.class};
        testNg.setListenerClasses(Arrays.asList(listenerClass));
        testNg.setTestClasses(new Class[]{CollectionExcute.class});
        testNg.run();
        responseVo.setIsSuccess(Boolean.TRUE);
        responseVo.setResult("执行完毕");return responseVo;
    }

有一个DataProvider_ForMysql类,作用是重写了Iterator,将集合内的所有接口以迭代器的形式通过DataProvider传入@Test中

CollectionExcute

public class CollectionExcute {@DataProvider(name = "testData")private Iterator getData(){return new DataProvider_ForMysql(ApiTestConfig.apiList);
    }@Test(dataProvider = "testData")public void testCase(TestCase testCase){
        Integer collectionId = ApiTestConfig.collectionId;// 返回结果的body
        String result = "";
        Response response = ApiTestUtils.doRequest(testCase, collectionId);try {
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }// 保存变量
        ApiTestUtils.saveVariable(result, testCase, collectionId);// 执行校验
        ApiTestUtils.verifyResult(result, testCase, collectionId);
    }
}

DataProvider_ForMysql

public class DataProvider_ForMysql implements Iterator<Object[]> {/** 查询结果集 */
    List caseList;/** 总行数 */private int rowNum=0;/** 当前行数 */private int curRowNo=0;public DataProvider_ForMysql(List cases){this.caseList = cases;this.rowNum = caseList.size();
    }@Overridepublic boolean hasNext() {if(rowNum==0||curRowNo>=rowNum){return false;
        }else{return true;
        }
    }@Overridepublic Object[] next() {
        TestCase testCase = caseList.get(curRowNo);
        Object[] o=new Object[1];
        o[0]=testCase;this.curRowNo++;return o;
    }
}

最后将执行完成后的“集合详情”数据返回给前端,让前端toast提示执行完毕,并更新报告的生成时间,此时点击“测试报告”按钮即可通过最新的url跳转至新的测试报告页面。

5ef321c718b2d8928db30e85533921f2.png

有疑问的小伙伴欢迎在文章下方留言,我会根据问题不断优化文章内容!

f7de3a95ec8a483afd6e5e8c6020af8b.png

989b970c4dccd4f5138a7b6ee0f4e7df.png

接口自动化测试平台简介

接口自动化测试平台中接口内容的设计

接口自动化测试平台:接口内容的简单执行

接口自动化测试平台系列:接口执行顺序与依赖

794ba7972232126b9c8f24837189c2f1.png

a24e579904f6028bf6fa782b771bd1fa.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值