Forum Discussion

LeonardoS's avatar
LeonardoS
Occasional Contributor
7 years ago

MongoDB as a data source for data driven testing

I want to use MongoDB as a data source for data-driven testing. I'm able to connect to MongoDB via Groovy Scripts and I'm able to retrieve data. But I'm trying to figure out how to use that data as a...
  • JHunt's avatar
    7 years ago

    So you already have the data and just want to iterate over it? This is a common thing to want to do.

     

    Add the WSDL or REST resource to your project, and create a Request in the interface definition, and set up the request as a kind of template for the requests you will make in your test. You don't need any TestRequests in your TestCase, just a groovy script.

     

    This code shows how to iterate over your data and make basic assertions against the response.

     

    myRequest = testRunner
     .getTestCase()
     .getProject()
     .getInterfaceByName('SampleServiceSoapBinding')
     .getOperationByName('buy')
     .getRequestByName('Request 1')
    
    assert myRequest.requestContent == '''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.soapui.org/sample/">
       <soapenv:Header/>
       <soapenv:Body>
          <sam:buy>
             <sessionid>?</sessionid>
             <buystring>${myVariable}</buystring>
          </sam:buy>
       </soapenv:Body>
    </soapenv:Envelope>'''
    
    testData = [ [in: "A", out: "D"],[in: "B", out: "E"],[in: "C", out: "F"] ]
    
    testData.each { test ->
         
         context.myVariable = test.in
         
         myRequest
          .submit(context, false)
          .getResponse()
          .with { 
          	if (it.contentAsString.contains(test.out)) {
          		log.info("Response for input of ${test.in} contains ${test.out}")
          	} else {
          		log.error("Response for input of ${test.in} did not contain ${test.out}")
          	}
          }
          
    }

    Notice how the test data for each run is added into the context, and then the context is passed into the call to submit.