Forum Discussion

sibaram_subudhi's avatar
sibaram_subudhi
Occasional Contributor
8 years ago
Solved

How to capture assertions from SoapUI Test steps while executing SoapUI Scripts from a java applicat

Hi,

 

I am working on implementing the SoapUI project execution using Java programming. I am able to get the test suites, test cases and test steps.

 

How can I capture the assertions defined in a step and their results.

 

Please suggest.

  • sibaram_subudhi's avatar
    sibaram_subudhi
    8 years ago

    Hi Rupert,

     

    Thanks for providing your inputs. However I implemented in similar way with different approach.

     

    // get a list of all test suites on the project
    suiteList = project.getTestSuiteList();

    // iterating through each suite
    for(int i = 0; i < suiteList.size(); i++){

    //Neglect if suite is disabled/inactive
    if(suiteList.get(i).isDisabled()){
    continue;
    }

    // get the test suite name000
    suiteName = suiteList.get(i).getName();

    System.out.println("Test Suite Is :" + suiteName );
    reportStr = reportStr + "\nTest Suite: " + suiteName;

    // get a list of all test cases from the test suite
    caseList = suiteList.get(i).getTestCaseList();

    WsdlTestCaseRunner runner;

    // iterating through each test case
    for(TestCase testcase : caseList){
    //neglect the disabled/inactive test case
    if (testcase.isDisabled())
    {
    continue;
    }

    //Getting the WsdlTestCaseRunner
    runner = new WsdlTestCaseRunner((WsdlTestCase) testcase, new StringToObjectMap(testcase.getProperties()) );

    //Running the Test Case
    runner.run();

    //Getting the result of each step
    List<TestStepResult> results = runner.getResults();

    System.out.println("Test Case : " + testcase.getName());

    //iterating through the results of the test steps executed in the test case
    for(TestStepResult result : results ){
    TestStepStatus stepStatus = result.getStatus();
    //printing the status , we can have a check here for OK , FAILED or UNKNOWN test cases
    System.out.println("Test Step Is : "+ result.getTestStep().getName() + " Test Step Status Is : " + stepStatus.toString());

    if (result instanceof WsdlTestRequestStepResult){
    if(result.getMessages().length>0){
    //Getting the error messages
    String[] errStr = result.getMessages();
    StringBuilder sb= new StringBuilder();

    //iterating through the error messages
    for(int ii=0;ii<errStr.length;ii++){
    String asserWithExcptn=errStr[ii];
    String assertName= asserWithExcptn.substring(0,asserWithExcptn.indexOf("]")+1);
    String assertResult= asserWithExcptn.substring(asserWithExcptn.indexOf("]")+1, asserWithExcptn.length());
    sb.append("The failed assertion #"+(ii+1)+" as follows.. \n");
    sb.append("The assertion is: "+assertName+"\n");
    sb.append("The message is:"+assertResult);
    sb.append("\n");
    }
    System.out.println(sb.toString());
    }
    }
    }
    reportStr = reportStr + "\n\tTestCase: " + testcase.getName() + "\tStatus: " + runner.getStatus() + "\tReason: " + runner.getReason();
    }
    }
    //'reportStr' has the status of all the test cases
    System.out.println("The Execution Status as follows: \n"+reportStr);

     

    The above script  will capture more specific messages(Failed assertions from each test step).

8 Replies

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    Sounds interesting...

     

    I have only really tried to capture Assertion results in a project level teardown script after executing all the tests - here is a simple version of a similar script:

     

    log.info "TestSuite Name,TestSuite Status,TestCase Name,TestCase Status,Error Messages(s)"
    for ( testSuiteResult in runner.results ){
    	
    	for ( testCaseResult in testSuiteResult.results )
    	{
    		def row=""
    		row=testCaseResult.testCase.name + ","+testCaseResult.status.toString()+","
        		for ( testStepResult in testCaseResult.getResults() )
        		{
          		row+=testStepResult.testStep.name +","+ testStepResult.status.toString()+","
    			testStepResult.messages.each() { message -> row+=message
    			}
    		}
    		log.info row
    	}
    
    }

    The Assertion failure messages show up in the messages collection, but not success messages show there.

     

    Hope this helps,

    Rupert

    • sibaram_subudhi's avatar
      sibaram_subudhi
      Occasional Contributor

      Hi Rupert,

       

      Thanks for providing your inputs. However I implemented in similar way with different approach.

       

      // get a list of all test suites on the project
      suiteList = project.getTestSuiteList();

      // iterating through each suite
      for(int i = 0; i < suiteList.size(); i++){

      //Neglect if suite is disabled/inactive
      if(suiteList.get(i).isDisabled()){
      continue;
      }

      // get the test suite name000
      suiteName = suiteList.get(i).getName();

      System.out.println("Test Suite Is :" + suiteName );
      reportStr = reportStr + "\nTest Suite: " + suiteName;

      // get a list of all test cases from the test suite
      caseList = suiteList.get(i).getTestCaseList();

      WsdlTestCaseRunner runner;

      // iterating through each test case
      for(TestCase testcase : caseList){
      //neglect the disabled/inactive test case
      if (testcase.isDisabled())
      {
      continue;
      }

      //Getting the WsdlTestCaseRunner
      runner = new WsdlTestCaseRunner((WsdlTestCase) testcase, new StringToObjectMap(testcase.getProperties()) );

      //Running the Test Case
      runner.run();

      //Getting the result of each step
      List<TestStepResult> results = runner.getResults();

      System.out.println("Test Case : " + testcase.getName());

      //iterating through the results of the test steps executed in the test case
      for(TestStepResult result : results ){
      TestStepStatus stepStatus = result.getStatus();
      //printing the status , we can have a check here for OK , FAILED or UNKNOWN test cases
      System.out.println("Test Step Is : "+ result.getTestStep().getName() + " Test Step Status Is : " + stepStatus.toString());

      if (result instanceof WsdlTestRequestStepResult){
      if(result.getMessages().length>0){
      //Getting the error messages
      String[] errStr = result.getMessages();
      StringBuilder sb= new StringBuilder();

      //iterating through the error messages
      for(int ii=0;ii<errStr.length;ii++){
      String asserWithExcptn=errStr[ii];
      String assertName= asserWithExcptn.substring(0,asserWithExcptn.indexOf("]")+1);
      String assertResult= asserWithExcptn.substring(asserWithExcptn.indexOf("]")+1, asserWithExcptn.length());
      sb.append("The failed assertion #"+(ii+1)+" as follows.. \n");
      sb.append("The assertion is: "+assertName+"\n");
      sb.append("The message is:"+assertResult);
      sb.append("\n");
      }
      System.out.println(sb.toString());
      }
      }
      }
      reportStr = reportStr + "\n\tTestCase: " + testcase.getName() + "\tStatus: " + runner.getStatus() + "\tReason: " + runner.getReason();
      }
      }
      //'reportStr' has the status of all the test cases
      System.out.println("The Execution Status as follows: \n"+reportStr);

       

      The above script  will capture more specific messages(Failed assertions from each test step).

      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Ok, no problem - thanks for sharing the solution back, could help others! :-)

         

        Thanks,

        Rup

    • sibaram_subudhi's avatar
      sibaram_subudhi
      Occasional Contributor

      Hello Rupert,

       

      Thanks for quick reply.

       

      I am also able to get the result for Test suite, test case and step wise. Here the challenge is to get the assertion(s) and their results. The message we are extracting from step result is not even meaningful. need to implement to capture stacktrace or a meaningful error.

      If you observe the screenshot, a test step may have more than 1 assertion and upon execution we will get the shown message. Trying to capture them as well. 

       

      Please suggest if you understood.

       

      Currently getting like [Ljava.lang.String;@729d6ee2 . 

       

      Thanks, Sibaram