[SoapUI] 从测试套件,测试用例,测试步骤,测试数据各个级别控制是否执行
自动保存
# -*- coding: utf-8 -*- import java.awt.Color import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.util.CellRangeAddress
import org.apache.poi.xssf.usermodel.XSSFCellStyle
import org.apache.poi.xssf.usermodel.XSSFColor
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.xssf.usermodel.XSSFDataValidationHelper
import org.apache.poi.xssf.usermodel.XSSFDataValidation
import org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint
import org.apache.poi.ss.util.CellRangeAddressList /* 1.获得所有测试套件,测试用例,测试步骤(可只选择发送Rest请求的步骤)的名字以及层级关系
** 2.分3个sheet页保存测试套件,测试用例,测试步骤的名字到Excel
** 3.这3个sheet页分别从测试套件,测试用例,测试步骤的级别增加一个Enable列,用来解析,以便通过SoapUI控制其是否执行
*/ String filePath = context.expand( '${projectDir}' ) + "/TestData/" + "TestSuiteTestCaseTestStepList.xlsx"
//String filePath = "D:/Automation_Develop_Work/SelectTestCaseToRunInSoapUI/TestSuiteTestCaseTestStepList.xlsx" //获取所有测试套件,测试用例,测试步骤的名称,存为linkedHashMap
LinkedHashMap<String, String> testSetMap = getTestSetMap(log,testRunner) //写入Excel
writeTestSetIntoExcel(filePath,testSetMap)
log.info "Save Success!" ///////////////////////////////////////////以下为封装的基础方法///////////////////////////////////////////////
// 获取所有的测试套件,测试用例,测试步骤的名称
static def getTestSetMap(log,testRunner){
def testSuiteList = testRunner.testCase.testSuite.project.getTestSuiteList()
def testCaseList
def testStepList
def testStepName LinkedHashMap<String, String> testSetMap = new LinkedHashMap<String, String>()
for(testSuite in testSuiteList){
//log.info "Test Suite : "+testSuite.name
testCaseList = testSuite.getTestCaseList() def testCaseMap = [:]
for(testCase in testCaseList){
//log.info "Test Case : "+testCase.name
// 只获取发送Rest请求的步骤,如果要获取所有测试步骤,改为testStepList = testCase.getTestStepList()
testStepList = testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep)
def testStepNameList = []
for (testStep in testStepList){
testStepName = testStep.name
testStepNameList.add(testStepName)
//log.info "Test Step : "+testStepName
}
testCaseMap.put(testCase.name,testStepNameList)
}
testSetMap.put(testSuite.name,testCaseMap)
}
//log.info testSetMap
return testSetMap
} // 将所有的测试套件,测试用例,测试步骤的名称写入Excel
static def writeTestSetIntoExcel(filePath,testSetMap){
XSSFWorkbook workbook = new XSSFWorkbook() XSSFSheet sheet1 = workbook.createSheet("Test Suite")
XSSFSheet sheet2 = workbook.createSheet("Test Case")
XSSFSheet sheet3 = workbook.createSheet("Test Step") //写第一个sheet页
def topRow1 = ["Test Suite","Enable"]
writeTitleIntoExcel(topRow1,workbook,sheet1)
writeTestSuiteIntoExcel(testSetMap,sheet1)
int columnNum1 = topRow1.size()
setSheetStyle(sheet1,columnNum1)
setAutoSizeColumn(sheet1,columnNum1) //写第二个sheet页
def topRow2 = ["Test Suite","Test Case","Enable"]
sheet2.setColumnWidth(0, 50*256)
sheet2.setColumnWidth(1, 80*256)
sheet2.setColumnWidth(2, 7*256)
writeTitleIntoExcel(topRow2,workbook,sheet2)
writeTestSuiteAndTestCaseIntoExcel(testSetMap,workbook,sheet2)
int columnNum2 = topRow2.size()
setSheetStyle(sheet2,columnNum2) //写第三个sheet页
def topRow3 = ["Test Suite","Test Case","Test Step","Enable"]
sheet3.setColumnWidth(0, 50*256)
sheet3.setColumnWidth(1, 50*256)
sheet3.setColumnWidth(2, 50*256)
sheet3.setColumnWidth(3, 7*256)
writeTitleIntoExcel(topRow3,workbook,sheet3)
writeTestSuiteTestCaseTestStepIntoExcel(testSetMap,workbook,sheet3)
int columnNum3 = topRow3.size()
setSheetStyle(sheet3,columnNum3) FileOutputStream fileOut = new FileOutputStream(filePath)
workbook.write(fileOut)
fileOut.flush()
fileOut.close()
} // 写title行
static def writeTitleIntoExcel(topRow,workbook,sheet){
XSSFCellStyle cellStyleTitle = setCellStyleTitle(workbook)
Row row = sheet.createRow(0)
int cellnum = 0
for (String cellString : topRow) {
Cell cell = row.createCell(cellnum++)
cell.setCellStyle(cellStyleTitle)
cell.setCellValue(cellString)
}
} // 写测试套件级别
static def writeTestSuiteIntoExcel(testSetMap,sheet){
String[] enableTextList = ["Y","N"]
int rownum = 1
def testSuitesList = testSetMap.keySet()
sheet.addValidationData(setDataValidation(sheet,enableTextList,1,testSuitesList.size(),1,1))
for (String testSuite : testSuitesList) {
int cellnum = 0
def row = sheet.createRow(rownum++)
Cell cell = row.createCell(cellnum++)
cell.setCellValue(testSuite)
Cell cellEnable = row.createCell(cellnum++)
cellEnable.setCellValue("Y")
}
} // 写测试用例级别 及其层级关系
static def writeTestSuiteAndTestCaseIntoExcel(testSetMap,workbook,sheet){
// 设置自动换行
XSSFCellStyle cellStyle = workbook.createCellStyle()
cellStyle.setWrapText(true) String[] enableTextList = ["Y","N"]
int rownum = 1
int testCasesTotalNumber = 0
def testSuitesList = testSetMap.keySet()
for (String testSuite : testSuitesList) {
def testCaseMap = testSetMap.get(testSuite)
def testCasesList = testCaseMap.keySet()
testCasesTotalNumber += testCasesList.size()
int i=0
for (String testCase : testCasesList) {
def testRow
if((i++)==0){
testRow = [testSuite,testCase,"Y"]
}
else{
testRow = ["",testCase,"Y"]
}
Row row = sheet.createRow(rownum++)
int cellnum = 0
for (String cellString : testRow) {
Cell cell = row.createCell(cellnum++)
cell.setCellValue(cellString)
cell.setCellStyle(cellStyle)
}
}
}
sheet.addValidationData(setDataValidation(sheet,enableTextList,1,testCasesTotalNumber,2,2))
} // 写测试步骤级别 及其层级关系
static def writeTestSuiteTestCaseTestStepIntoExcel(testSetMap,workbook,sheet){
// 设置自动换行
XSSFCellStyle cellStyle = workbook.createCellStyle()
cellStyle.setWrapText(true) String[] enableTextList = ["Y","N"]
int rownum = 1
int testStepsTotalNumber = 0
def testSuitesList = testSetMap.keySet()
for (String testSuite : testSuitesList){
def testCaseMap = testSetMap.get(testSuite)
def testCasesList = testCaseMap.keySet()
int i=0
for (String testCase : testCasesList){
def testStepsList = testCaseMap.get(testCase)
testStepsTotalNumber += testStepsList.size()
int j=0
for(String testStep : testStepsList){
def testRow
if(i==0){
testRow = [testSuite,testCase,testStep,"Y"]
}
else if (j==0){
testRow = ["",testCase,testStep,"Y"]
}
else{
testRow = ["","",testStep,"Y"]
}
Row row = sheet.createRow(rownum++)
int cellnum = 0
for (String cellString : testRow) {
Cell cell = row.createCell(cellnum++)
cell.setCellValue(cellString)
cell.setCellStyle(cellStyle)
}
j++
i++
}
}
}
sheet.addValidationData(setDataValidation(sheet,enableTextList,1,testStepsTotalNumber,3,3))
} // 设置sheet的样式
static def setSheetStyle(XSSFSheet sheet, int columnsNum) {
sheet.createFreezePane(0, 1, 0, 1) //冻结第一行
String columnRange = "A1:" + (char) (65 + (columnsNum-1)) + "1"
sheet.setAutoFilter(CellRangeAddress.valueOf(columnRange)) //设置自动筛选
// for (int i = 0; i <= columnsNum; i++)
// sheet.autoSizeColumn(i) //自动调整列宽
return sheet
} // 自动调整列宽
static def setAutoSizeColumn(XSSFSheet sheet, int columnsNum) {
for (int i = 0; i <= columnsNum; i++)
sheet.autoSizeColumn(i) //自动调整列宽
return sheet
} // 设置Title行的样式
static def setCellStyleTitle(XSSFWorkbook workbook) {
XSSFCellStyle cellStyle = workbook.createCellStyle()
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND)
cellStyle.setFillForegroundColor(new XSSFColor(new Color(34, 139, 34))) //设为绿色背景
return cellStyle
} static def setDataValidation(sheet,textList,firstRow,endRow,firstCol,endCol) { XSSFDataValidationHelper helper = sheet.getDataValidationHelper()
// 加载下拉列表内容
XSSFDataValidationConstraint constraint = helper.createExplicitListConstraint(textList)
constraint.setExplicitListValues(textList) // 设置数据有效性加载在哪个单元格上
// 四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList((short) firstRow, (short) endRow, (short) firstCol, (short) endCol) // 数据有效性对象
XSSFDataValidation dataValidation = helper.createValidation(constraint, regions)
return dataValidation
}
控制测试套件,测试用例,测试步骤的执行
# -*- coding: utf-8 -*- import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook String filePath = context.expand( '${projectDir}' ) + "/TestData/" + "TestSuiteTestCaseTestStepList.xlsx"
//String filePath = "D:/Automation_Develop_Work/SelectTestCaseToRunInSoapUI/TestSuiteTestCaseTestStepList4.xlsx"
FileInputStream fileInputStream = new FileInputStream(filePath)
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream) def testSuiteMap = getInfoFromExcel(workbook) //遍历Test Suite级别
def testSuiteList = testRunner.testCase.testSuite.project.getTestSuiteList()
for(testSuite in testSuiteList){
def testSuiteName = testSuite.getName()
log.info "Test Suite Name : "+testSuiteName
def testSuiteValue = testSuiteMap.get(testSuiteName)
def testSuiteEnable = testSuiteValue.get("enable")
log.info "Test Suite Enable : "+testSuiteEnable
if(testSuiteEnable == "Y"){
testSuite.setDisabled(false)
}
if(testSuiteEnable == "N"){
testSuite.setDisabled(true)
}
def testCaseMap = testSuiteValue //遍历Test Case级别
def testCaseList = testSuite.getTestCaseList()
for(testCase in testCaseList){
def testCaseName = testCase.getName() def testCaseValue = testCaseMap.get(testCaseName) log.info "Test Case Name : "+testCaseName
log.info "Test Case Value : "+testCaseValue
def testCaseEnable = "Y"
try{
testCaseEnable = testCaseValue.get("enable")
}
catch (Exception e){
testCaseEnable = "N"
}
log.info "Test Case Enable : "+testCaseEnable
if(testCaseEnable == "Y"){
testCase.setDisabled(false)
}
if(testCaseEnable == "N"){
testCase.setDisabled(true)
}
def testStepMap = testCaseValue //遍历Test Step级别
def testStepList = testCase.getTestStepList()
def testStepEnableNumForRestRequest = 0
// Rest请求的enable状态根据excel里面的填的进行设置
for(testStep in testStepList){
if (testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep){
def testStepName = testStep.getName()
def testStepValue
try{
testStepValue = testStepMap.get(testStepName)
}
catch (Exception e){
testStepValue = ["enable":"N"]
}
log.info "Test Step Name : "+testStepName
log.info "Test Step Value : "+testStepValue
def testStepEnableForRestRequest = testStepValue.get("enable")
log.info "Test Step Enable : "+testStepEnableForRestRequest
if(testStepEnableForRestRequest == "Y"){
testStep.setDisabled(false)
testStepEnableNumForRestRequest += 1
}
if(testStepEnableForRestRequest == "N"){
testStep.setDisabled(true)
}
}
} //非Rest请求的状态需要特殊处理,只要当前test case下面有一个Rest 请求要求是enable的,所有非Rest请求都设为Enable的
def testStepEnableForNonRestRequest = "N"
if(testStepEnableNumForRestRequest>0){
testStepEnableForNonRestRequest = "Y"
}
for(testStep in testStepList){
if (!(testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep)){
if(testStepEnableForNonRestRequest == "Y"){
testStep.setDisabled(false)
}
if(testStepEnableForNonRestRequest == "N"){
testStep.setDisabled(true)
}
}
}
}
} ///////////////////////////////////////////以下为封装的基础方法///////////////////////////////////////////////
// 从三个sheet页获取test suite,test case,test step的信息
private def getInfoFromExcel(XSSFWorkbook workbook) {
def testSuiteMap = [:]
// 先处理第3个sheet页,再处理第2个,最后处理第1个,谁的信息最多,就先处理谁
getInfoFromThirdSheet(workbook, testSuiteMap)
getInfoFromSecondSheet(workbook, testSuiteMap)
getInfoFromFirstSheet(workbook, testSuiteMap)
return testSuiteMap
} // 处理第三个sheet页
private void getInfoFromThirdSheet(XSSFWorkbook workbook, LinkedHashMap testSuiteMap) {
def testCaseMap = [:]
def testStepMap = [:] XSSFSheet sheet = workbook.getSheetAt(2)
int lastRowNum = sheet.getLastRowNum()
String testSuite = ""
String testCase = ""
for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) { XSSFRow row = sheet.getRow(rowNum)
String firstCellInRow = row.getCell(0).getStringCellValue()
String secondCellInRow = row.getCell(1).getStringCellValue()
if (firstCellInRow != "") {
testSuite = firstCellInRow
testCaseMap = [:]
}
if (secondCellInRow != "") {
testCase = secondCellInRow
testStepMap = [:]
}
String testStep = row.getCell(2).getStringCellValue()
String enable = row.getCell(3).getStringCellValue() def enableMap = ["enable": enable]
testStepMap.put(testStep, enableMap)
testCaseMap.put(testCase, testStepMap)
testSuiteMap.put(testSuite, testCaseMap)
}
} // 处理第二个sheet页
private void getInfoFromSecondSheet(XSSFWorkbook workbook, LinkedHashMap testSuiteMap) {
XSSFSheet sheet = workbook.getSheetAt(1)
int lastRowNum = sheet.getLastRowNum()
String testSuite = ""
for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
XSSFRow row = sheet.getRow(rowNum)
String firstCellInRow =""
try{
firstCellInRow = row.getCell(0).getStringCellValue()
}
catch (NullPointerException e){
// 第一个单元格为null时,不去通过getStringCellValue()获取单元格的内容,而是直接引用初始值
}
// 因为test suite和前面的一样时,excel单元格那里是空着的,所以需要保存前面test suite的名字用于填充map
if (firstCellInRow != "") {
testSuite = firstCellInRow
}
String testCase = row.getCell(1).getStringCellValue()
String enable = row.getCell(2).getStringCellValue() // 当我们只将发送Rest请求的test step写入excel时,有可能某些test suite或test case下面一个发送rest请求的步骤都没有
// 这种情况在处理第一个和第二个sheet页的时候会出现找不到对应此test suite和test case的key,因为我们是从第3个sheet页开始处理生成map的
if(!testSuiteMap.containsKey(testSuite)){
def testCaseMap = [:]
testCaseMap.put(testCase,["enable": enable])
testSuiteMap.put(testSuite, testCaseMap)
}
if(!testSuiteMap.get(testSuite).containsKey(testCase)){
testSuiteMap.get(testSuite).put(testCase,["enable": enable])
}
else{
testSuiteMap.get(testSuite).get(testCase).put("enable",enable)
}
}
} // 处理第一个sheet页
private void getInfoFromFirstSheet(XSSFWorkbook workbook, LinkedHashMap testSuiteMap) {
XSSFSheet sheet = workbook.getSheetAt(0)
int lastRowNum = sheet.getLastRowNum()
for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
XSSFRow row = sheet.getRow(rowNum)
String testSuite = row.getCell(0).getStringCellValue()
String enable = row.getCell(1).getStringCellValue()
// 第1个sheet页存在的test suite在第2个和第3个sheet页都不存在时,需要做特殊处理
if(!testSuiteMap.containsKey(testSuite)){
testSuiteMap.put(testSuite, ["enable": enable])
}
else{
testSuiteMap.get(testSuite).put("enable", enable)
}
}
}
控制测试数据的执行
# -*- coding: utf-8 -*- /* 1.假定Data Source格式为Excel
** 2.在Excel中增加一列Enable
** 3.读取这个Enable列的值,如果为Y,运行,如果为N,不运行
*/ def ifEnable = context.expand('${DataSource#Enable}')
def testRunnerResult = '''${=testRunner.results[testRunner.results.size()-1].status}''' //如果此行ifEnable=="N",不运行此行测试数据,将DataSink中result设置为SKIP,并跳转到DataSink运行
if(ifEnable=="N"){
context.testCase.getTestStepByName("DataSink").setPropertyValue("result", "SKIP")
testRunner.gotoStepByName("DataSink")
}
//如果此行ifEnable=="Y",运行此行测试数据,将DataSink中result设置为当前testRunner的结果
else
context.testCase.getTestStepByName("DataSink").setPropertyValue("result", testRunnerResult)
[SoapUI] 从测试套件,测试用例,测试步骤,测试数据各个级别控制是否执行的更多相关文章
- unittest如何制作测试套件Testsuite 按method,class ,module,按命令行执行unittest
cmd 执行unittest case : python -m unitest test.py test2.py python -m unittest testfile.testclass.tes ...
- 『心善渊』Selenium3.0基础 — 28、unittest中测试套件的使用
目录 1.测试套件的作用 2.使用测试套件 (1)入门示例 (2)根据不同的条件加载测试用例(了解) (3)常用方式(推荐) 1.测试套件的作用 在我们实际工作,使用unittest框架会有两个问题: ...
- unittest测试套件
测试套件就是测试集,测试集是测试用例的集合. a.按用例顺序执行(addtest) 当addtest与unittest的测试规则冲突时,仍然按照ASCII码的顺序执行. import unittest ...
- Python+Selenium笔记(四):unittest的Test Suite(测试套件)
(一) Test Suite测试套件 一个测试套件是多个测试或测试用例的集合,是针对被测程序的对应的功能和模块创建的一组测试,一个测试套件内的测试用例将一起执行. 应用unittest的TestSui ...
- Robot Framework(四)创建测试套件
2.3.1测试用例文件 Robot Framework测试用例是使用测试用例文件中的测试用例表创建的.这样的文件会自动从它包含的所有测试用例中创建一个测试套件.可以有多少测试用例没有上限,但建议少于1 ...
- python+unittest框架第二天unittest之简单认识Test Suite:测试套件
今天了解下测试套件Test Suite,什么是测试套件,测试套件是由多个Test Case测试用例组成的,当然也可以由多个子测试套件组成. 接下来看下如果构建测试套件,构建测试套件的方法: 1.用un ...
- junit测试套件
在实际项目中,随着项目进度的开展,单元测试类会越来越多,可是直到现在我们还只会一个一个的单独运行测试类,这在实际项目实践中肯定是不可行的.为了解决这个问题,JUnit 提供了一种批量运行测试类的方法, ...
- [Azure DevOps] 管理测试计划、测试套件和测试用例
我喜欢测试计划,它能让团队清楚测试进度,还能妥善分配测试人员,更重要的是它能保证测试质量和效率.Azure DevOps 里提供了 Test Plans 这个模块用于管理测试计划. 1. Azure ...
- RIDE-工程、测试套件、测试用例三者关系
理论 type的选择: 一般来说:测试项目(directory)-测试套件(file)-测试用例 本质上,“测试项目”和“测试套件”并没有什么区别,但是testcase只能放在file类型的test ...
随机推荐
- Delphi LiveBinds组件
Component Logo Component Name Description TBindSourceDB Is used for creating bindings to databases. ...
- WIN7X64SP1极限精简版by双心
WIN7X64SP1极限精简版by双心 http://bbs.wuyou.net/forum.php?mod=viewthread&tid=405044&page=1&ext ...
- 前端-JavaScript1-2——JavaScript建立认知
关于首篇的“ Hello world ! ”这事儿吧,挺有意思,就是学习任何的语言,我们都喜欢在屏幕上直接输出一点什么,当做最简单.最基本的案例.输出什么大家随意,但是很多人都习惯输出“hello w ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
- time&datetime
关于time模块的代码部分 1 #_*_coding:utf-8_*_ 2 __author__ = 'Alex Li' 3 4 import time 5 6 7 # print(time.cloc ...
- Python-递加计数器
计数本:number.txt 1 2 3 4 主程序:计数器 # Author: Stephen Yuan # 递加计算器 import os # 递加计算器 def calc(): file_siz ...
- Alpha阶段项目规划
Alpha阶段任务 概述 我们团队采访学长并听从学长的意见之后,决定根据第一版的phylab项目进行重构.但由于第一版的phylab项目在github上的代码仅仅只有alpha版本,我们接手之后进行了 ...
- Ajax的异步与同步(async)
1.async值为true (异步)当ajax发送请求后,在等待server端返回的这个过程中,前台会继续 执行ajax块后面的脚本,直到server端返回正确的结果才会去执行success,也就是说 ...
- 使用httpClient发送post请求连接restful接口
public static String httpPost(String url,String arg){ InputStream is; BufferedReader br; StringBuild ...
- Entity Frame Code First 简易教程
简介 什么是ORM 搭建Entity FrameWork CodeFirst应用 数据库迁移 表属性常见配置 Entity FrameWork 一对多.多对多 一.简介 Entity Framewor ...