Forum Discussion

nemowbray's avatar
nemowbray
New Contributor
3 years ago
Solved

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 

  • nmrao's avatar
    nmrao
    3 years ago

    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'

     

     

     

6 Replies

  • 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.")
    • nemowbray's avatar
      nemowbray
      New Contributor

      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's avatar
      nemowbray
      New Contributor

      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.")

       

      • nmrao's avatar
        nmrao
        Champion Level 3

        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'

         

         

         

  • nmrao's avatar
    nmrao
    Champion Level 3
    Because, the expected value is just logged, but needs to be asserted.