Forum Discussion

pjw's avatar
pjw
Occasional Contributor
8 years ago

Iterate through the TestSuites in my Project TearDown Script (using Groovy Script)

Hello.

 

All I want to do to start, is iterate through the TestSuites in my Project TearDown Script (using Groovy Script) and get the Name and Status of each TestSuite (whether it Passed or Failed). If I can get more details (test cases/test steps that failed etc) that would be a bonus.

 

I can not find an example or the properties to do this and have tried multiple methods.

 

Does anyone have the code to do this please?

 

Many thanks,

Paul

 

P.S. This is the best I've got so far but there must be a better/cleaner way to approach this so I can get the results too?

 

import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;

import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.model.support.PropertiesMap;
import com.eviware.soapui.model.testsuite.TestCase;
import com.eviware.soapui.model.testsuite.TestRunner;
import com.eviware.soapui.model.testsuite.TestRunner.Status;
import com.eviware.soapui.model.testsuite.TestSuite;

 

def testCaseName = ""

def testSuiteRunner = context["#TestSuiteRunner#"]


if( testSuiteRunner != null )

{
    for(r in testSuiteRunner.results )
    testCaseName += testSuiteRunner.results.testSuite.name
}

 

 

 

3 Replies

  • Hello Paul,

     

    I have the same question. Please let me know if you are aware of any solution.

     

    Cheers,

    Sheela

  • jmueller's avatar
    jmueller
    New Contributor

    You were mostly there, it looks like you were only getting the name in your script is that right?

    The property I think you are looking for is the status property of the various result classes. I put this in my tear down script and it worked well.

     

    for (suiteResult in runner.results) {
    log.info(suiteResult.testSuite.name + " " + suiteResult.status)
    for (caseResult in suiteResult.results) {
    log.info("\t" + caseResult.testCase.name + " " + caseResult.status)
    for (stepResult in caseResult.results) {
    log.info("\t\t" + stepResult.testStep.name + " " + stepResult.status)
    }
    }
    }

     

    You can write to a file instead of using log.info if you want to save this output for some type of reporting.

     

    Jack 

  • pjw's avatar
    pjw
    Occasional Contributor

    Hi All.

     

    Thanks for comments/help on this. Thanks to people in other posts too for part of the email code.

     

    Here is my full solution that I hope will help others too :-)

     

    This will loop through each Test Suite in a project getting the Test Suite name and its status, then send a summary email.

     

    Put the code below in your PROJECT Teardown Script.

     

    Also, if/when you Launch TestRunner, select to output the report to a root folder (which will be included in the email).This code could be improved by taking the input parameters and setting the report output location dynamically. Each environment that you have set up will output to a different location (You may need to create the .../EnvironmentName/ProjectName/ directories in the root location)

     

    I set the To Email Address(es) as a Project Property but have hardcoded in the code below.

     

    I set up a scheduled job which calls a .bat file to Launch the Test Runner so that this runs test checking our environments regularly. It only emails when it detects failures but you can change this easily. I intend to move this to using QAComplete and the Scheduler.

     

    I am using ReadyAPI with SoapUI Pro.

     

    Cheers,

    Paul

     

    -------------------------------------------------------------------------------------------------------

    import javax.mail.internet.*;
    import javax.mail.*
    import javax.activation.*

    import static org.junit.Assert.assertEquals;
    import java.util.List;
    import org.junit.Test;

    import com.eviware.soapui.impl.wsdl.WsdlProject;
    import com.eviware.soapui.model.support.PropertiesMap;
    import com.eviware.soapui.model.testsuite.TestCase;
    import com.eviware.soapui.model.testsuite.TestRunner;
    import com.eviware.soapui.model.testsuite.TestRunner.Status;
    import com.eviware.soapui.model.testsuite.TestSuite;

    def testCaseName = ""
    def testSuiteNames = ""
    def failedCount = 0
    def results = "" //project details and failed results
    def passedResults = ""
    def message = ""
    def testSuiteRunner = context["#TestSuiteRunner#"]

    results += "ReadyAPI Project : <b>" + project.name + "</b></br>"
    results += "Environment : <b>" + project.activeEnvironment.name + "</b></br>"
    results += "Project File : " + project.path + "</br>"
    results += "\n<a href=C:\\Your\\Report\\Output\Root\\Folder\\Results\\" + project.activeEnvironment.name + "\\" + project.name + "\\index.html" + ">See the Latest report here</a></br></br>";
    results += "<b>Failed Test Suites" + "</b>" + "</br>"
    passedResults = "<b>Completed Test Suites</b>" + "</br>"

    if( testSuiteRunner != null ){
    for(r in testSuiteRunner.results )
    testSuiteNames = testSuiteRunner.results.testSuite.name
    for (int i = 0; i < testSuiteNames.size(); i++) {
    String testSuiteStatus = testSuiteRunner.results.get(i).status
    if (testSuiteStatus.equals("FAILED")){
    testCaseName = (testSuiteNames.get(i)) + " <b style=color:red>" + testSuiteStatus + "</b></br>";
    failedCount ++;
    results += testCaseName;
    } else {
    testCaseName = (testSuiteNames.get(i)) + " <b style=color:green>" + testSuiteStatus + "</b></br>";
    passedResults += testCaseName;
    }
    }
    }

    if (failedCount > 0){
    subject = project.activeEnvironment.name + " Environment : " + project.name + " Testing Failed"
    toAddress = project.getPropertyValue( "SendEmailFailuresTo" )
    fromAddress = "Automated_Tests@Yoursmtphost.com"
    host = "Yoursmtphost.com"
    port = "25" //You may use a different port and possibly username/pwd

    Properties mprops = new Properties();
    mprops.setProperty("mail.transport.protocol","smtp");
    mprops.setProperty("mail.host",host);
    mprops.setProperty("mail.smtp.port",port);

    message += results + "</br>";
    message += passedResults

    Session lSession = Session.getDefaultInstance(mprops,null);
    MimeMessage msg = new MimeMessage(lSession);

    //tokenize out the recipients in case they came in as a list
    StringTokenizer tok = new StringTokenizer(toAddress,";");
    ArrayList emailTos = new ArrayList();
    while(tok.hasMoreElements()){
    emailTos.add(new InternetAddress(tok.nextElement().toString()));
    }
    InternetAddress[] to = new InternetAddress[emailTos.size()];
    to = (InternetAddress[]) emailTos.toArray(to);
    msg.setRecipients(MimeMessage.RecipientType.TO,to);
    InternetAddress fromAddr = new InternetAddress(fromAddress);
    msg.setFrom(fromAddr);
    msg.setFrom(new InternetAddress(fromAddress));
    msg.setSubject(subject);
    msg.setText(message, "UTF-8", "html");

    Transport transporter = lSession.getTransport("smtp");
    transporter.connect();
    transporter.send(msg);
    }