Forum Discussion

cnanju's avatar
cnanju
Occasional Contributor
8 years ago

How to cope with the changing schema of request/response or both to test rest APIs?

Hi,

     We have a test project for Rest API, enormous test cases have been written to validate multiple test cases.

Due to change in requirement, Request/Response or both may change. Which will make the existing test cases to fail and changing the existing test cases is time consuming and laborious. How to tackle this issue?

 

Change in response may include or remove an existing field of the JSON response.

  • Hi,

     

    Apart from doing something hacky like a find n replace in the project.xml file (which often works fine!), I would imagine a Groovy based solution might work for you. It isn't a complete solution, but I created this script in response to a similar question a while back:

     

    https://gist.github.com/bearsoftware/cfed4594dc3ae1ce7a419ca5fc9b5747

     

    /*
     * For a chosen REST Service POST request:
     * 
     * -> Manually specify a new request body
     * -> Update the request of ALL (POST) REST TestRequest TestSteps contained in ALL TestCases of a chosen TestSuite
     */
    
    //Set TestSuite to update here:
    
    def testSuiteNameToUpdate = "TestSuite 2"
    
    //Set new request body here:
    
    def requestBodyToCopy = """{
      "apiPostId" : "125",
      "data" : [ {
        "name" : "email",
        "value" : "aaaacccchtomosanya@yahoo.com"
      }, {
        "name" : "data_source",
        "value" : "Outlet"
      }, {
        "name" : "date_of_record",
        "value" : "2016-04-18T00:05:57Z"
      }, {
        "name" : "marketing_opt_in_mens",
        "value" : "1"
      }, {
        "name" : "marketing_opt_in_womens",
        "value" : "1"
      }, {
        "name" : "marketing_opt_in_kids",
        "value" : "0"
      }, {
        "name" : "email_sp3_status_id",
        "value" : "100"
      } ]
    }"""
    
    //Get chosen TestSuite
    
    def project = testRunner.getTestCase().getProject()
    def testSuiteToUpdate = project.getTestSuiteByName(testSuiteNameToUpdate)
    
    //Select ALL TestCases
    
    def testCasesToUpdate = testSuiteToUpdate.getTestCaseList()
    
    def updateCount=0
    //For each TestCase
    testCasesToUpdate.each{testCase ->
    
    	log.info "--Checking TestCase: $testCase.name"
    	//Select ALL RESTTestRequest TestSteps
    	def testStepsToUpdate = testCase.getTestStepList()
    	//For each TestStep
    	testStepsToUpdate.each{testStep ->
    		log.info testStep.name
    		//Is it a POST request?
    		if (testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep && 
    			testStep.restMethod.method.toString()=="POST"){
    			log.info "Updating request of TestStep $testStep.name ..."
    			testStep.getHttpRequest().setRequestContent(requestBodyToCopy)
    			updateCount++
    		}
    		
    	}
    
    }
    return "Total of $updateCount REST TestRequest TestSteps have been updated."

    Maybe it can be adapted to your needs or form the basis for further conversation?

     

    Regards,

    Rupert

  • cnanju's avatar
    cnanju
    8 years ago

    Hi Rupert,

     

                          I hope that would just help to fix the problem, I guess the Schema inference and Data driven driven tests(to centralize a test case with multiple input/output validation) feature of SAOP UI would be the right candidate going further to fix such schema issues.

  • cnanju's avatar
    cnanju
    8 years ago

    I am actually trying trial version of "SoapUI Pro/ Rady!API 1.9.0 "for this.

    And Schema Inference is definitely helping it out.

    As you mentioned for the scripts same kind of feature like "Auto Update Schema" menu option/ button to update the schema for all test cases or test suites or the project would be a great feature. 

     

    As I am using trial version of "SoapUI Pro/ Rady!API 1.9.0 "  @Community please let me know if any such feature is there already or in pipeline.

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Apart from doing something hacky like a find n replace in the project.xml file (which often works fine!), I would imagine a Groovy based solution might work for you. It isn't a complete solution, but I created this script in response to a similar question a while back:

     

    https://gist.github.com/bearsoftware/cfed4594dc3ae1ce7a419ca5fc9b5747

     

    /*
     * For a chosen REST Service POST request:
     * 
     * -> Manually specify a new request body
     * -> Update the request of ALL (POST) REST TestRequest TestSteps contained in ALL TestCases of a chosen TestSuite
     */
    
    //Set TestSuite to update here:
    
    def testSuiteNameToUpdate = "TestSuite 2"
    
    //Set new request body here:
    
    def requestBodyToCopy = """{
      "apiPostId" : "125",
      "data" : [ {
        "name" : "email",
        "value" : "aaaacccchtomosanya@yahoo.com"
      }, {
        "name" : "data_source",
        "value" : "Outlet"
      }, {
        "name" : "date_of_record",
        "value" : "2016-04-18T00:05:57Z"
      }, {
        "name" : "marketing_opt_in_mens",
        "value" : "1"
      }, {
        "name" : "marketing_opt_in_womens",
        "value" : "1"
      }, {
        "name" : "marketing_opt_in_kids",
        "value" : "0"
      }, {
        "name" : "email_sp3_status_id",
        "value" : "100"
      } ]
    }"""
    
    //Get chosen TestSuite
    
    def project = testRunner.getTestCase().getProject()
    def testSuiteToUpdate = project.getTestSuiteByName(testSuiteNameToUpdate)
    
    //Select ALL TestCases
    
    def testCasesToUpdate = testSuiteToUpdate.getTestCaseList()
    
    def updateCount=0
    //For each TestCase
    testCasesToUpdate.each{testCase ->
    
    	log.info "--Checking TestCase: $testCase.name"
    	//Select ALL RESTTestRequest TestSteps
    	def testStepsToUpdate = testCase.getTestStepList()
    	//For each TestStep
    	testStepsToUpdate.each{testStep ->
    		log.info testStep.name
    		//Is it a POST request?
    		if (testStep instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep && 
    			testStep.restMethod.method.toString()=="POST"){
    			log.info "Updating request of TestStep $testStep.name ..."
    			testStep.getHttpRequest().setRequestContent(requestBodyToCopy)
    			updateCount++
    		}
    		
    	}
    
    }
    return "Total of $updateCount REST TestRequest TestSteps have been updated."

    Maybe it can be adapted to your needs or form the basis for further conversation?

     

    Regards,

    Rupert

    • cnanju's avatar
      cnanju
      Occasional Contributor

      Hi Rupert,

       

                            I hope that would just help to fix the problem, I guess the Schema inference and Data driven driven tests(to centralize a test case with multiple input/output validation) feature of SAOP UI would be the right candidate going further to fix such schema issues.

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        Well, I'm not sure if there is any out-of--the-box functionality in the open source version of SoupUI that can directly tackle this problem.

         

        However, the problem you're highlighting i.e. changing rest request in the definition and then having a 'Replace All' button to update all related REST TestSteps would be a nice feature request! (see SoapUI Feature Requests Quite doable.

         

        Thanks,

        Rupert