Data-driven testing with SoapUI Free Edition


Having spent more than 3 years in a “Web Service”-heavy development team, I can safely say that SmartBear’s SoapUI is a great tool; so great in fact that I have found myself becoming some sort of SoapUI-evangelist, much to the despair of some of my colleagues.

Whereas the focus of the Pro Edition of SoapUI is geared towards data-driven testing, the truth is that its price tag can be a hard pill to swallow, especially for smaller companies. The fact however remains that you can do a lot of the data-driven stuff on the free edition, as long as you have a very basic understanding of Groovy and maybe a bit of imagination.

Prior to the day that my Development Manager had enough of me preaching about SoapUI and bought us a few licences for the Pro edition, I managed to put together a couple of scripts that would allow me to simulate data-driven testing based on the capabilities of the free edition. Having struggled myself to find complete and relevant examples online, I thought it might be useful to share some of those scripts through an example case scenario.

The Use Case

Let’s assume that we have a Soap Method GetArticleXml(int ContentId, string UserId), which requires a single integer (ContentId) and a User Identified (UserId) in order to respond successfully.

In this case we want the ContentId to be dynamic, for example originating from an external data source such as a text file. Additionally, we want the application to read the data source entries in a random format so that we can “make things a bit more realistic”.

Step 1 – Preparing the Data Source

In this example the data source will be a simple text file where each ContentId is on its own line. Assuming that your Content IDs are originally stored in a database, it should be pretty straight forward to export it in this format. Something as simple as the example below would do (no line numbers required):

029389
132165
321321
378976
654654
664232
654333
654665

Following this, go ahead and save your file, giving it an imaginative name such as “data.txt”. To add insult to injury, in this example this file will be stored (and referred to) in the… “C:\Temp” folder.

Step 2: Setting up the Test Case

Having set up our “data source” in the form of a text file, the next step will be to set up a Test Case that will be used for our purposes. If you haven’t done it yet, create a new Test Suite (Right Click | Generate TestSuite) and ensure that a Test Case has been generated for the method you are interested in (in our example, GetArticleXml).

Enter the Test Case Edit mode and double-click the Sample Request that has been created for you automatically.

As mentioned before, the dynamic part of the request is the ContentId whereas the UserId is static. As such, I’ll go ahead and set the UserId to an appropriate value (e.g. “Spongebob”)

At this stage we want to start introducing the “plumbing” for the dynamic data source. We will achieve this by declaring a property within the scope of the Test Case – this will enable us to assign a value to this property and then use a variable at any part/step of the Test Case which will resolve to the value of this property.

In our case, such property could be called “DynamicContentId”. To define this property follow the steps below:

  1. On the Test Case Editor, click on the “Properties” tab (bottom left) and expand.
  2. Click on the “+” icon to add a new property.
  3. Provide the Unique Property Name – in our example “DynamicContentId” and press OK.

Step 3: The Groovy Bit!

The next step will be to write some code that will read random lines from your data source (the text file) and will assign those IDs to the recently created dynamic property.

Please note: I have not coded seriously for approximately 24 months and even then Groovy was not my forte. As such, usage of the following code snippet is not recommended for medical equipment, nuclear facilities, submarines and oil rigs.


//Load the text file
 def inputFile = new File("C://Temp//data.txt");

//Create an empty list...
 def UrlList = [];

//...and then populate it with the contents
 // of the text file.
 addURLstoList = {UrlList.add(it)};
 inputFile.eachLine(addURLstoList);

//Randomly pick an item from the list...
 def randomIndex = (int)Math.random()*UrlList.size;
 def randomURL = UrlList.get(randomIndex);

//...and assign its value to the Test Case Property
 // titled "DynamicContentId"
 def tc = testRunner.testCase;
 tc.setPropertyValue("DynamicContentId", randomURL);

The code snippet above should be pasted in the “Setup Script” tab of your Test Case (bottom of the case window). The Setup Script is a script which is run every time prior to the execution of the Test Case, as opposed to the “Tear Down Script” which is run after the execution of the last Test Case step.

As such, with this setup script in place, every time the Test Case is run, the property “DynamicContentId” will be assigned a value randomly by the setup script (if in doubt, read the code comments).

Step 4: Passing the Dynamic Property to the SOAP Request

However, the purpose of this exercise is not to pass dynamic values to a SoapUI Test Case property but to pass dynamic values to the actual Soap Request. As such, a mechanism is required that will allow us to “inject” the property in the request – this can be done with the help of a special syntax provided to us by SoapUI.


<soapenv:Envelope xmlns:soapenv="..." xmlns:art="...">
   <soapenv:Header/>
     <soapenv:Body>
       <art:GetArticleXml>
       <!--Optional:-->
         <art:ContentId>${#TestCase#DynamicContentId}</art:ContentId>
         <!--Optional:-->
         <art:UserId>Spongebob</art:UserId>
      </art:GetArticleXml>
   </soapenv:Body>
</soapenv:Envelope>

The variable used in the XML above “${#TestCase#DynamicContentId}” is a simple way to instruct the request to read its value from the Test Case property titled “DynamicContentId”.

Since this property is dynamically and randomly populated from the Data Source (with the help of the setup script), the value that will be injected to the request will be also dynamic and random.

Step 5 : Let’s make things a bit faster…

As you might have realised, at this stage the test is only run every time we execute the Test Case manually – not ideal if you have a Data Source with thousands of entries and no one to spend all day clicking away on a mouse button.

What we need is a way of executing the test case for a controlled number of times (runs) or over a specific time period or by more than one users at the same time (threads)… For this purpose, you can simply create a SoapUI Load Test, based on your Test Case and execute the Load Test.

The Test Case will execute for the specified time period/amount of runs, while every time it runs the DynamicContentId property gets re-evaluated.

To create a new Load Test based on your Test Case, right-click on the Test Case on the project Navigator and choose LoadTest. Simples!

Tagged , ,

12 thoughts on “Data-driven testing with SoapUI Free Edition

  1. great guide! i’m not understanding step 4 though. where do you paste that code? everything else works great! thanks

    • Eldar says:

      Fantastic article 🙂 Very practical and useful. Unfortunately, following the steps I have failed to achieve the desired. Can you show some screenshots?

  2. Neda khazai says:

    Great Post, my name is Neda from the SmartBear soapUI team. I’m writing to ask if you would interested in blogging for SmartBear’s software quality blog . We’re looking for exceptional bloggers with experience with soapUI and you seem to fit the bill.
    If you’re interested, send me an email to Neda.Khazai@smartbear.com and we can discuss further.

    Regards
    Neda

    • David Pursehouse says:

      Great way to engage with your audience, Neda. 🙂
      SoapUI is growing on me fast, despite it’s little niggles. I found I couldn’t talk between test suites until I gave up and went to close SoapUI – then I was asked to save my projects, so I did, then I retried my project which went straight through – lesson learnt, save all before running.

  3. Sarat says:

    Excellent post. Could you also post a script for reading with excel and databases please.

    Thanks,
    Sarat

  4. Rohit Jaiswal says:

    I am getting error while following step 4 code snippet which says “MulitpleCompilationErrorsException”. Could you also provide scripts for xls files.

    • roman says:

      Hi, I know this was a long time ago. But how did you manage to resolve this one??

      • Abderrahmane says:

        I had a similar problem in different context I think if you did a copy/ paste the problem come from this line :
        tc.setPropertyValue("DynamicContentId", randomURL);
        you should replace each &quot by “

  5. Asit Tripathy says:

    For excel file please copy the .jar file ( Apache POI or JXL ) inside the library folder of the installation location of SOAP UI. That will solve the issue if the code works fine and you wont get the COMPILE error.

    import org.apache.poi.hssf.usermodel.*;

    cellDataFormatter = new HSSFDataFormatter()

    //Create formula evaluator
    fEval = new HSSFFormulaEvaluator(context.srcWkSheet.getWorkbook())

    //Increment the rowcounter then read in the next row of items
    context.rowCounter++;

    if(context.rowCounter<=context.srcWkSheet.getLastRowNum()){//Check if we've reached the last row
    curTC = testRunner.testCase
    sourceRow = context.srcWkSheet.getRow(context.rowCounter)//Get a spreadsheet row

    //Step through cells in the row and populate property data

  6. lohith p menon says:

    I could import values from a text file but how do I run the script for multiple times. For eg: i have a data file with 10 inputs and I need to run the script twice, can you please let me know how to do it.

    Thanks;
    Lo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: