我正在运行一些API测试,并且正在使用groovy脚本来获取测试运行报告(SoapUI免费版本)。我的问题是报告采用.csv格式,尽管我需要使用.json格式。
首先,报告以.csv格式生成,但是现在我们必须切换到json,我无法弄清楚要更改为.json格式的脚本的内容。
try {
// 1. Create a "SoapUIResults" folder in the project path
// Retrieve the project root folder
def projectPath = new com.eviware.soapui.support.GroovyUtils(context).projectPath
// Specify a folder inside project root to store the results
String folderPath = projectPath + "/SoapUIResults";
// Create a File object for the specified path
def resultFolder = new File(folderPath);
// Check for existence of folder and create a folder
if(!resultFolder.exists())
{
resultFolder.mkdirs();
}
/* ------------------------------------------------------------------------------- */
// 2. Create a subfolder (with timestamp) to store the request-response local copy
// Retrieve the latest execution date-time
Date d = new Date();
def executionDate = d.format("dd-MMM-yyyy HH_mm");
// Specify the subfolder path with name Request-Response_CurrentTimeStamp
String subfolderPath1 = folderPath+ "/Request-Response_"+executionDate;
// Create this sub-folder
new File(subfolderPath1).mkdirs();
/* ------------------------------------------------------------------------------- */
// 3. Create another subfolder "JSON Reports" to store the reports file
// Specify the subfolder path with name JSON Reports
String subfolderPath2 = folderPath+ "/JSON Reports";
// Create this sub-folder
new File(subfolderPath2).mkdirs();
/* ------------------------------------------------------------------------------- */
// 4. Create a Report.json file inside the JSON Reports folder
// Create a File object for Report json file (with timestamp)
def reportFile = new File(subfolderPath2, "Report_"+executionDate+".json");
// Check for existence of report file and create a file
if(!reportFile.exists())
{
reportFile.createNewFile();
// Create required column names in the report file
reportFile.write('"Test Suite","Test Case","Test Step","Step Type","Step Status",'
+'"Result message","Execution Date"');
}
/* ------------------------------------------------------------------------------- */
// 5. Inserting data in the file
// Iterate over all the test steps results
for(stepResult in testRunner.getResults())
{
// Retrieve Test Suite name
def testSuite = testRunner.testCase.testSuite.name;
// Retrieve Test Case name
def testCase = testRunner.testCase.name;
// Retrieve Test Step
def testStep = stepResult.getTestStep();
// Retrieve Test Step name
def testStepName = testStep.name
// Retrieve Test Step type
def type = testStep.config.type
// Retrieve Test Step status
def status = stepResult.getStatus()
// Creating new line in report file
reportFile.append('\n');
// Write all the necessary information in the file
reportFile.append('"' + testSuite + '",');
reportFile.append('"' + testCase + '",');
reportFile.append('"' + testStepName + '",');
reportFile.append('"' + type + '",');
reportFile.append('"' + status + '",');
// Retrieve the test result messages
reportFile.append('"');
for(resMessage in stepResult.getMessages())
{
// Write messages and separate multiple messages by new line
reportFile.append('Message:' + resMessage + '\n');
}
reportFile.append('",');
//Write executionDate in the file
reportFile.append('"' + executionDate + '",');
/* ------------------------------------------------------------------------------- */
// 6. Extract the request and response and save it to external file
// Verify if the test step type is request: SOAP project or restrequest: REST project
if((type=="request").or(type=="restrequest"))
{
// Extract the request from the test step
def extRequest = testStep.properties["Request"].value;
// Create a file in the reports folder and write the request
// Naming convention: request_TestSuiteName_TestCaseName_TestStepName.txt
def requestFile=subfolderPath1+"/request_"+testSuite+"_"+testCase+"_"+testStepName+".txt";
def rqfile = new File(requestFile);
rqfile.write(extRequest, "UTF-8");
// Extract the response from the test step
def extResponse = stepResult.getResponseContent();
// Create a file in the reports folder and write the response
// Naming convention: response_TestSuiteName_TestCaseName_TestStepName.txt
def responseFile=subfolderPath1+"/response_"+testSuite+"_"+testCase+"_"+testStepName+".txt";
def rsfile = new File(responseFile);
rsfile.write(extResponse, "UTF-8");
}
}
}
catch(exc)
{
log.error("Exception happened: " + exc.toString());
}
我希望输出是一个json文件,但它是一个.csv文件。
编辑: TearDown脚本如下:
// Code to execute the GenerateJSONReport test step
testRunner.testCase.testSuite.project.testSuites["Library"].testCases["Reporting_Utility"].
testSteps["GenerateJSONReport"].run(testRunner, context);