cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script to run a Teststep doesn't fail when the test Step fails

SOLVED
nemowbray
New Contributor

Groovy script to run a Teststep doesn't fail when the test Step fails

Instead of copying and pasting test steps that I am using over and over in other test cases i decided to use a grovvy step to call the test step.  When one of the assertions fail on the step the grovvy step is not failed but instead the test continues onto the next step.  I am at a loss for how to get the step to fail when the test step called has a failure in one or more of the assertions.  Any help would be appreciated.

 

Here is the groovy step I am using: 

 

//Generate Authentication Token
// Run the test step
def auth = testRunner.testCase.testSuite.getTestCaseByName("followup_email - Valid English First").getTestStepByName("Generate Auth Token")

//Execute Test Step
auth.run(testRunner, context)

//CHECK FAILED COMMUNICATIONS
// Run the test step
def tStep = testRunner.testCase.testSuite.getTestCaseByName("followup_email - to: Field is Missing").getTestStepByName("Check for Failed Communications")

//Execute Test Step
tStep.run(testRunner, context)

//Results
log.info(tStep.getAssertionStatus())
log.info(tStep.getAssertableContentAsXml())

 

When log.info(tStep.getAssertionStatus()) comes back FAIL the groovy step is not failed 

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: Groovy script to run a Teststep doesn't fail when the test Step fails

@nemowbray 

All you need is just a line in order to check if the particular test step us passed or not.

Rremove every thing after line 10 of your script and add below line.

Assuming that  the expected result is pass.

 

 

assert tStep.run(testRunner, context).status.toString()  in ['OK', 'UNKNOWN'], ' Test step status does not match with the expected value'

 

 

 

In case you are expecting tStep to be failed, the use below assertion

 

 

 

assert tStep.run(testRunner, context).status.toString() == 'FAILED', 'Test step status does not match the expected result'

 

 

 



Regards,
Rao.

View solution in original post

6 REPLIES 6
nmrao
Community Hero

Re: Groovy script to run a Teststep doesn't fail when the test Step fails

Because, the expected value is just logged, but needs to be asserted.


Regards,
Rao.
HimanshuTayal
Community Hero

Re: Groovy script to run a Teststep doesn't fail when the test Step fails

@nemowbray : In order to achieve this you need to assert it in the groovy script which is calling that common Test Step:

 

 

def result = testRunner.testCase.testSteps["TEST_STEP_NAME"].run(testRunner, context).getStatus().toString()

if ( result == "OK")
	testRunner.pass("Test Script passed.")
else
	testRunner.fail("Test Script failed.")

Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
nemowbray
New Contributor

Re: Groovy script to run a Teststep doesn't fail when the test Step fails

@HimanshuTayal I added an assert but still the test would not fail, but using your solution has worked!  Seems i was overthinking it.  Thanks!

nemowbray
New Contributor

Re: Groovy script to run a Teststep doesn't fail when the test Step fails

Hi @HimanshuTayal

 

Seems i jumped the gun as I thought it was working but it wasn't.  Its failing every case even though the test passes.

 

//CHECK FAILED COMMUNICATIONS
// Run the test step
def tStep = testRunner.testCase.testSuite.getTestCaseByName("followup_email - Valid English First").getTestStepByName("Check for Failed Communications")

//Execute Test Step
tStep.run(testRunner, context).getStatus().toString()

//Results
log.info(tStep.getAssertionStatus())
log.info(tStep.getAssertableContentAsXml())

//ASSERT
def result = tStep.getAssertionStatus()
log.info(result)
if (result == "PASS")
testRunner.pass("Test Script passed.")
else
testRunner.fail("Test Script failed.")

nemowbray_0-1623946052745.png

 

HimanshuTayal
Community Hero

Re: Groovy script to run a Teststep doesn't fail when the test Step fails

@nemowbray : try with

 

if(result.toString() == "PASS"){

//your logic

}

else{

//your logic

}

 

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
nmrao
Community Hero

Re: Groovy script to run a Teststep doesn't fail when the test Step fails

@nemowbray 

All you need is just a line in order to check if the particular test step us passed or not.

Rremove every thing after line 10 of your script and add below line.

Assuming that  the expected result is pass.

 

 

assert tStep.run(testRunner, context).status.toString()  in ['OK', 'UNKNOWN'], ' Test step status does not match with the expected value'

 

 

 

In case you are expecting tStep to be failed, the use below assertion

 

 

 

assert tStep.run(testRunner, context).status.toString() == 'FAILED', 'Test step status does not match the expected result'

 

 

 



Regards,
Rao.

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors