Friday, February 7, 2014

Data Driven Testing in soapUI open source

Preparing the test environment 


  • Create a folder where SOAP data driven testing will occur, e.g. C:\SOAPUIRulesTest 
  • In the project folder (e.g. DTTest) is where the SOAPUI project will be stored 
  • In the project folder, create a result folder for each instance of the testing you want to run, e.g. output
  • In the date/time stamped test case folder is where the following will reside 
    • input CSV file 
    • results XML files from SOAPUI 
    • results spreadsheet Preparing the input CSV file 
    • Create a CSV file into the test case folder, e.g. input.csv 
    • Format the CSV file as follows: 
    • First row is the name of tags which the values will be replaced with 
    • Row 2 onward are the tag values 
    • The last column is the expected result field - this is used to create an assertion in SOAPUI against which to test

soapUI Project 


  • Open soapUI and create a new project using the WSDL path for the Rule Service - keep the name short as long names may cause path issues down the line 
  • Create the test suite as part of the project creation or afterwards

  • Save the project into theproject folder created earlier (not the date/time stamped folder) Test Suite
  • Create a new test suite

Test Case 


  • Create a new test case - keep the name short line "TestCase1" - this name will be used later


  • Select the test case and select options


  • Make sure you unselect "Abort on Error" Test Request  Add Test Request and select "Create Optional Elements"


  • You should now see the SOAP XML structure, e.g.


  • For all "?" items in tag values, replace with substitution strings like "${#TestCase#TAGNAME}"
Setup Script 

  • In the test case open the "Setup Script" tab and paste the following code inside 

//Setup script: 
 //def directory = "C:/SOAPUIRulesTest/getClientRiskAppetiteClassification" def directory = "c:\{path}/result" 
 //Put readers/writers into context using the context variable so it can be used between test components 

context.fileReader = new BufferedReader(new FileReader(directory+"/"+"input.csv")) 
context.fileWriter = new File(directory+"/"+"output.csv") 
context.fileWriter.delete() context.resultscount = 0 

 //Read in the first line of the data file firstLine = context.fileReader.readLine() 

//Split the first line into a string array and assign the array elements to the field name array 

String[] strArray = firstLine.split(",") 
context.fieldNameArray = strArray 

 //Read in the second line of the data file 
secondLine = context.fileReader.readLine() 
String[] propData = secondLine.split(",")
 //Output the headings 

p=0 
for (i=0; i<context.fieldNameArray.size(); i++) 

{
       context.fileWriter.append(context.fieldNameArray[i]+",")
}
context.fileWriter.append("TestResult"+"\r\n")

//set the properties for the first record
p=0
for (i=0; i<context.fieldNameArray.size(); i++)
{
                testCase.setPropertyValue(context.fieldNameArray[i],propData[p++].trim()) //e.g. "uniqueId" = propData[0])
} 
p=0
for (i=0; i<context.fieldNameArray.size()-1; i++)
{
                context.fileWriter.append(propData[i]+",")
}

//Rename request test steps for readability in the log; append the element name to the test step names
testCase.getTestStepAt(0).setName("TestRequest_" + propData[0]+"_"+propData[propData.size()-1])



  • Change the file path to where the CSV file is stored (in the date/time stamped folder)
  • Adjust all the "setPropertyValue" statements to match the xml tags mentioned in the Test Request substitutions earlier

Groovy Script (ReadNextLine)
  • Now add a Groovy script called "ReadNextLine"

  •  Now paste the following code into the window for the script: 


//Setup script: 
 //def directory = "C:/SOAPUIRulesTest/getClientRiskAppetiteClassification" 
def directory = "C://result" 



 //Put readers/writers into context using the context variable so it can be used between test components context.fileReader = new BufferedReader(new FileReader(directory+"/"+"input.csv")) 
context.fileWriter = new File(directory+"/"+"output.csv") 
context.fileWriter.delete() 
 context.resultscount = 0 

 //Read in the first line of the data file 
firstLine = context.fileReader.readLine() 
//Split the first line into a string array and assign the array elements to the field name array 
String[] strArray = firstLine.split(",") 
context.fieldNameArray = strArray 

 //Read in the second line of the data file 
secondLine = context.fileReader.readLine() 
String[] propData = secondLine.split(",") 

//Output the headings 
p=0 
for (i=0; i<context.fieldNameArray.size(); i++) 
{
       context.fileWriter.append(context.fieldNameArray[i]+",")
}
context.fileWriter.append("TestResult"+"\r\n")

//set the properties for the first record
p=0
for (i=0; i<context.fieldNameArray.size(); i++)
{
testCase.setPropertyValue(context.fieldNameArray[i],propData[p++].trim()) //e.g. "uniqueId" = propData[0])
}

p=0
for (i=0; i<context.fieldNameArray.size()-1; i++)
{
                context.fileWriter.append(propData[i]+",")
}

//Rename request test steps for readability in the log; append the element name to the test step names
testCase.getTestStepAt(0).setName("TestRequest_" + propData[0]+"_"+propData[propData.size()-1])


Adjust all the "setPropertyValue" statements to match the xml tags mentioned in the Test Request substitutions earlier

Assertion
  • Click on the Assertion tab for the Test Request


Add details as:


  • This sets the assertion to match the last column of the CSV file
  • Run the test case to make sure all tests now pass




You can also run the test case in test runner and capture every request-response

No comments:

Post a Comment