Forum Discussion

chanukya's avatar
chanukya
Occasional Contributor
6 years ago

rerun a test case in ready api using tear down script

I have a test case "Login" which intermittently fails due to login issues. I would like to implement a tear down script to get the status of the script and rerun if it failed.

Here is what I implemented and it doesn't work as expected

 

testRunner.testCase.setPropertyValue("LoginStatus", 
testRunner.getStatus().toString())

def loginStatus = context.expand( '${#TestCase#LoginStatus}' )

int retryAttempts = context.expand( '${#Project#RetryAttempts}' ).toInteger()

def myContext = (com.eviware.soapui.support.types.StringToObjectMap)context

while ( loginStatus == "FAIL" && retryAttempts <= 1)  {
  retryAttempts = retryAttempts+1
  log.info "increment retry attempts-" + retryAttempts
  testRunner.testCase.testSuite.project.setPropertyValue( "RetryAttempts", 
   retryAttempts.toString() )
  testCase.run(myContext, false)
  log.info "after run statement-"+retryAttempts
}
log.info "before final statement"
testRunner.testCase.testSuite.project.setPropertyValue( "RetryAttempts", "0" 
)
LOGS- 

Fri May 18 13:55:15 EDT 2018:INFO:increment retry attempts-1 Fri May 18 13:55:16 EDT 2018:INFO:increment retry attempts-2 Fri May 18 13:55:16 EDT 2018:INFO:before final statement Fri May 18 13:55:16 EDT 2018:INFO:after run statement-2 Fri May 18 13:55:16 EDT 2018:INFO:before final statement Fri May 18 13:55:16 EDT 2018:INFO:after run statement-1 Fri May 18 13:55:16 EDT 2018:INFO:increment retry attempts-2 Fri May 18 13:55:17 EDT 2018:INFO:before final statement Fri May 18 13:55:17 EDT 2018:INFO:after run statement-2 Fri May 18 13:55:17 EDT 2018:INFO:before final statement

Question: How does the teardown script behave?

I am expected a Failed test at the first run. So it has to jump to run the test again and either go or exit since we reached the limit. But it runs 3 times. 

 

Could some one explain how tear down script works.

6 Replies

  • It seems intuitive that the teardown script is intended to do cleanup work for the test case or test suite after it executes. Have you tried this code in other scripts - like a requestFilter.afterRequest event handler or a groovy step? Maybe by the time teardown occurs a rerun of the test might have unpredictable results.

     

    I think of scripts running in this order

    1. Test Suite                 (setup script if it exists )

    2.     requestFilter.filterRequest (if the event handler  exists)

    3.     Test Case             (setup script if it exists)

    4.          Groovy Step    (groovy script if it exists)

    5.          requestFilter.AfterRequst (if the event handler exists)

    6..     Test Case closure (Assertion Script -if it exists)

    7..     Test Case             (teardown script if it exists)

    8.  Test Suite               (teardown script if it exists)

     

    Of course there are other event handler scripts but I normally work with the request filter events.

     

    I have been able to use the afterRequest and access the response and rerun a request.

     

    Bill

    • richie's avatar
      richie
      Community Hero
      Bill_In_Irvine

      thats a great explanation.....might be obvious to some people but thats laid it out for me quite nicely!

      cheers,

      richie
    • chanukya's avatar
      chanukya
      Occasional Contributor

      Hi,

       

      Thanks for your response.

       

      Could you please provide me a sample code that would allow to rerun a test from teardown script.

  • chanukya's avatar
    chanukya
    Occasional Contributor

    Can you give an example code on how to rerun a test case from tear down script?