cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Community Manager

[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

DifficultyStar_Gold.pngStar_Gold.pngStar_gray.png

 

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😊


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

How to Generate Email When Assertion Fails

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);
}

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Tags (1)
6 REPLIES 6
Highlighted
Community Hero

Re: Your Input Required - How to Generate Email When Assertion Fails

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.


Regards,
Rao.
Moderator

Re: Your Input Required - How to Generate Email When Assertion Fails

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. 



Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted
Community Manager

Re: Your Input Required - How to Generate Email When Assertion Fails

Thank you for the comment Rao!

 

Who else would like to participate?🙂

 

@msiadak @richie @krogold @HimanshuTayal @Radford 


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted
Community Hero

How to Generate Email When Assertion Fails

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);
}

 




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Tags (1)
Highlighted
Community Hero

Re: Your Input Required - How to Generate Email When Assertion Fails

Hey @sonya_m 

 

I wont be trying this - this is way beyond my coding skills - I wouldn't know where to start - its just too different to what I've tried before - (however, I'm more than happy to steal @msiadak's code for future reference) - but I cant wait for next week's task!

 

nice one,

 

rich

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Community Manager

Re: Your Input Required - How to Generate Email When Assertion Fails

@msiadak great solution! 

As for the disappeared comment - the spam filter got you. This was not normal behavior and I made sure that never happens to you again🙂

 

@richie Glad to hear this! I am about to post a new task🙂


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors