介面自動化測試平台系列:場景化執行
- 2019 年 11 月 22 日
- 筆記
前端很簡單,點擊「執行」按鈕即可,會像後端傳入該集合的id

後端拿到集合id後,會執行以下幾步操作:
- 拿到所有全局變數
- 拿到集合變數(集合變數只會在該集合內生效,不會在其他集合中生效)
- 聲明測試報告的保存路徑
- 更新集合的最後執行人與最後執行時間
- 調用testng類,進行介面執行,並生成報告
Service
@Override public ResponseVo collectionExcute(Integer collectionId) { ResponseVo responseVo = new ResponseVo(); ApiTestConfig.apiList = apiTestCaseMapper.selectCollectionApiList(collectionId); ApiTestConfig.collectionId = collectionId; // 全局變數賦值 List<Variable> globalVariableList = apiTestConfigMapper.selectGlobalVariable(); for (Variable variable:globalVariableList){ ApiTestConfig.globalVariableMap.put(variable.getVariableName(),variable.getVariableValue()); } // 集合變數賦值 List<Variable> 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<Object[]>,將集合內的所有介面以迭代器的形式通過DataProvider傳入@Test中
CollectionExcute
public class CollectionExcute { @DataProvider(name = "testData") private Iterator<Object[]> 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<TestCase> caseList; /** 總行數 */ private int rowNum=0; /** 當前行數 */ private int curRowNo=0; public DataProvider_ForMysql(List<TestCase> cases){ this.caseList = cases; this.rowNum = caseList.size(); } @Override public boolean hasNext() { if(rowNum==0||curRowNo>=rowNum){ return false; }else{ return true; } } @Override public Object[] next() { TestCase testCase = caseList.get(curRowNo); Object[] o=new Object[1]; o[0]=testCase; this.curRowNo++; return o; } }
最後將執行完成後的「集合詳情」數據返回給前端,讓前端toast提示執行完畢,並更新報告的生成時間,此時點擊「測試報告」按鈕即可通過最新的url跳轉至新的測試報告頁面。
