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 an input for my API validations... Because the SoapUI data source only allows executing SQL sentences. In my case, I have all the data I need in Groovy variables, how can use now this data as a part of an API request? Thank you so much in advance, believe me, I've tried everything before asking...

  • 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.

  • JHunt's avatar
    JHunt
    Community Hero

    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.

  • LeonardoS's avatar
    LeonardoS
    Occasional Contributor

    Thank you SO much for your response, I appreciate it, at least I know that my approach is feasible.

    But still, the solution is not clear to me, sorry for my ignorance.

     

    Suppose that I have the following simple code:

     

    import com.gmongo.GMongo
    
    def mongo = new GMongo()
    def db = mongo.getDB('test')          	
    def results = db.myCollection.findOne()
    	
    def username = results.USERNAME
    def city = results.CITY
    def password = results.PASSWORD

    I already sent the request to my data source (mongodb) and I already have the variables for my API test (username, city and password).

     

    How can I integrate USERNAME, CITY and PASSWORD to my test cases?

    Do I need to create a new script?

    Can I perform my request in the same script?

     

    Sorry for my ignorance, but I'm little lost, I don't know how to organize the structure of my test cases.

     

    Thank you again!