Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago

[TechCorner Challenge #6] How to Generate Email When Assertion Fails

Hello Community!

 

Today we have another task for you to help contribute to our collection of useful ReadyAPI content.

Here is the task: Create a Groovy script that will send an email when an assertion fails

Difficulty

 

The idea of the script is to run a send email test step when the assertion fails. Please note that the email should be sent if any assertion for any test step in a project fails.

To complete the task you should write a script for one of the events.

 

Good luck😊

  • Task: Create a Groovy script that will send an email when an assertion fails

     

    This is a solution created for [TechCorner Challenge #6]

     

    I thought I posted a reply but it disappeared.

     

     

    // set project reference
    def project = context.project;
    
    // set testTypes. Can expand for more test step types that allow assertions.
    def testTypes = [com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep, com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep];
    
    // start an error list.
    def errors = [];
    
    // Walk through the test suites, test cases, and test steps.
    for (ts in project.getTestSuiteList())
    {
    	for (tc in ts.getTestCaseList())
    	{
    		for (testType in testTypes)
    		{
    			for (step in tc.getTestStepsOfType(testType))
    			{
    				for (assertion in step.getAssertionList())
    				{
    					if (assertion.errors != null)
    					{
    						def errorMsg = ts.getName() + " - " + tc.getName() + " - " + step.getName();
    						errorMsg += " has failed with the error: " + assertion.errors.toString();
    						errors.add(errorMsg);
    					}
    				}
    			}
    		}
    	}
    }
    
    if (errors != [])
    {
    	// def sendMail = context.project.testSuites["TestSuiteWithSendMail"].testCases["TestCaseWithSendMail"].testSteps["Send Mail"];
    	def body = "";
    	for (error in errors)
    	{
    		body+= error + "\n";
    	}
    	def sendMail = context.testCase.testSteps["Send Mail"];
    	sendMail.setMessage(body);
    	sendMail.setSubject("Project run Errors");
    	sendMail.run(context.testRunner, context);
    }
    

     

  • nmrao's avatar
    nmrao
    Champion Level 3
    Just a comment.

    Running a test step in events does not seem to be natural fit. Because, event can be generated from any suite, test case or test step and where should the email test step be defined?

    In general, the CI/CD does the job of sending email irrespective of test execution completed successfully or not. IMO, it may not be right idea to send an email for an assertion failure as if may bloat inbox if failures are more on a bad day/ configuration etc.
    • Nastya_Khovrina's avatar
      Nastya_Khovrina
      SmartBear Alumni (Retired)

      Hi nmrao,

       

      Yes, I agree with you that this task isn't so realistic ðŸ˜‰

      But, maybe, this is a regression test for some basic functionality that always should pass. 

       

      Or, we can make a task more accurate - send an email only if an assertion for the "Very Important" Test Step fails.