Forum Discussion

jgoguen's avatar
jgoguen
Occasional Contributor
8 years ago
Solved

Log output from tests added with Groovy

Hi,

 

I have a SoapUi project with a Groovy test step that generates a series of test suites from a CSV. The idea is that each suite would have a set of request steps with a different request type from our wsdl. It all works well, however when running from the command line with testRunner, the normal log output that you would get from test suites that were already in the project don't appear. Only a log message that it was sending the request and received the response.

 

The requests are all generated with, at a minimum, Soap Response and Not Soap Fault assertions, but even this output does not appear. I assume this is because the test suites were added after the project starts running and, perhaps, that the log only prints for test suites/cases/steps that were already there when it started. Is there a way around this?

  • So, I figured it out on my own. I'll post the resolution just in case someone else finds it useful. Let me start with a more precise description of the problem:

     

    If you have a project that contains groovy code that adds a test case with requests in it, the default log output you would normally get with such a test case is absent when the test case runs. Specifically, this is the log output in the "soapUI log" area in the GUI, which also gets printed when running on the command line with testrunner.sh.

     

    import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapResponseAssertion
    import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.NotSoapFaultAssertion
    import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
    import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
    import com.eviware.soapui.impl.wsdl.*
    import groovy.util.slurpersupport.GPathResult
    import groovy.xml.XmlUtil
    
    
    WsdlProject project = context.testCase.testSuite.project
    
    // Add test suite/case
    WsdlTestSuite ts = project.addNewTestSuite("My Test Suite")
    WsdlTestCase tc = ts.addNewTestCase("My Test Case")
    
    // Build a request from an interface operation
    WsdlInterface intfc = project.interfaces["someWsdlInterfaceBinding"]
    WsdlOperation op = intfc.getOperationByName("someWsdlOperation")
    String requestString = op.createRequest(true).toString()
    GPathResult envelope = new XmlSlurper().parseText(requestString)
    
    // Now you can populate values in the request envelope if necessary
    GPathResult body = envelope.Body["someWsdlOperation"]
    body.someNode = "someText"
    body.someNestedNode.someChildNode = "someMoreText"
    
    // Create the WsdlRequest and add your content
    WsdlRequest request = operation.addNewRequest("My New Request")
    XmlUtil xmlUtil = new XmlUtil()
    requestString = xmlUtil.serialize(envelope)
    request.setRequestContent(requestString)
    
    // Now, add a request test step with your request to the new test case
    def config = WsdlTestRequestStepFactory.createConfig(request, "My New Test Step")
    def ts = tc.insertTestStep(config, -1)
    
    // Add some assertions to the new step
    ts.testRequest.addAssertion((new SoapResponseAssertion.Factory()).getAssertionLabel())
    ts.testRequest.addAssertion((new NotSoapFaultAssertion.Factory()).getAssertionLabel())

    The above code will add the new test suite/case/step to your project, which will immediately run after the groovy step completes. But once the new test case runs, you will notice the only output you see is that the request was sent and the response received. It will not output when it starts/finishes the test case nor whether or not the assertions passed.

     

    The reason for this is that there is a set of listeners that gets added to each test case once the project is run. These listeners are responsible for the normal test case output and any assertion output that you would normally see in the soapUI log. Any test case added after the project starts to run does not contain these listeners and, therefore, lacks the log output. The solution, then, is to add these listeners to any new test case dynamically added to the project. Don't worry, though, you don't have to know specifically which listeners or how to instantiate them. Just copy the ones from your groovy step's test case.

     

    ...
    
    // Add test suite/case
    WsdlTestSuite ts = project.addNewTestSuite("My Test Suite")
    WsdlTestCase tc = ts.addNewTestCase("My Test Case")
    
    // Add listeners to your new case
    context.testCase.testRunListeners.each
    {
       tc.addTestRunListener(it)
    }
    
    ...

    Now you will see the normal log output in the soapUI log and on the command line for your new case.

2 Replies

  • jgoguen's avatar
    jgoguen
    Occasional Contributor

    So, I figured it out on my own. I'll post the resolution just in case someone else finds it useful. Let me start with a more precise description of the problem:

     

    If you have a project that contains groovy code that adds a test case with requests in it, the default log output you would normally get with such a test case is absent when the test case runs. Specifically, this is the log output in the "soapUI log" area in the GUI, which also gets printed when running on the command line with testrunner.sh.

     

    import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapResponseAssertion
    import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.NotSoapFaultAssertion
    import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory
    import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
    import com.eviware.soapui.impl.wsdl.*
    import groovy.util.slurpersupport.GPathResult
    import groovy.xml.XmlUtil
    
    
    WsdlProject project = context.testCase.testSuite.project
    
    // Add test suite/case
    WsdlTestSuite ts = project.addNewTestSuite("My Test Suite")
    WsdlTestCase tc = ts.addNewTestCase("My Test Case")
    
    // Build a request from an interface operation
    WsdlInterface intfc = project.interfaces["someWsdlInterfaceBinding"]
    WsdlOperation op = intfc.getOperationByName("someWsdlOperation")
    String requestString = op.createRequest(true).toString()
    GPathResult envelope = new XmlSlurper().parseText(requestString)
    
    // Now you can populate values in the request envelope if necessary
    GPathResult body = envelope.Body["someWsdlOperation"]
    body.someNode = "someText"
    body.someNestedNode.someChildNode = "someMoreText"
    
    // Create the WsdlRequest and add your content
    WsdlRequest request = operation.addNewRequest("My New Request")
    XmlUtil xmlUtil = new XmlUtil()
    requestString = xmlUtil.serialize(envelope)
    request.setRequestContent(requestString)
    
    // Now, add a request test step with your request to the new test case
    def config = WsdlTestRequestStepFactory.createConfig(request, "My New Test Step")
    def ts = tc.insertTestStep(config, -1)
    
    // Add some assertions to the new step
    ts.testRequest.addAssertion((new SoapResponseAssertion.Factory()).getAssertionLabel())
    ts.testRequest.addAssertion((new NotSoapFaultAssertion.Factory()).getAssertionLabel())

    The above code will add the new test suite/case/step to your project, which will immediately run after the groovy step completes. But once the new test case runs, you will notice the only output you see is that the request was sent and the response received. It will not output when it starts/finishes the test case nor whether or not the assertions passed.

     

    The reason for this is that there is a set of listeners that gets added to each test case once the project is run. These listeners are responsible for the normal test case output and any assertion output that you would normally see in the soapUI log. Any test case added after the project starts to run does not contain these listeners and, therefore, lacks the log output. The solution, then, is to add these listeners to any new test case dynamically added to the project. Don't worry, though, you don't have to know specifically which listeners or how to instantiate them. Just copy the ones from your groovy step's test case.

     

    ...
    
    // Add test suite/case
    WsdlTestSuite ts = project.addNewTestSuite("My Test Suite")
    WsdlTestCase tc = ts.addNewTestCase("My Test Case")
    
    // Add listeners to your new case
    context.testCase.testRunListeners.each
    {
       tc.addTestRunListener(it)
    }
    
    ...

    Now you will see the normal log output in the soapUI log and on the command line for your new case.