lihi_h3
lihi_h3

Reputation: 61

How to fail a test with conditions in Robot Framework

I have trouble writing if conditions in Robot Framework. I need to Know if a process is failed\succeeded\still in progress. I have a loop with timeout that waits until the process is failed\succeed(done)

I not sure how to : - brake from a case and fail test - only if the process failed.
- brake from a case and pass test - only if the process "succeed"\done.

here is the python code:

   for i in range(timeout):
       if wait_for_failed_proccess is True:
          result = False
          break 
       if wait_for_success_process is True:
          result = True
          break 
       time.sleep(1000)
   return result

robot framework code:

${result} =    Test process waiter
Run keyword if| ${result}==False---> need to fail test. the process has failed  
Run keyword if| ${result}==True---> test passed. continue to the next test 

Test process waiter
 [documentation]          wait until process is done
 [timeout]                25 min 

 For      ${index}     IN RANGE     [TIMEOUT]
  run keyword if|Validate failed process==Ture|${result}=False|Exist From loop 
  run keyword if|Validate success process==Ture|${result}=True|Exist From loop
  Sleep      10
 END
 [return] result 


Validate failed process
 [documentation]        confirmed process failed 
 Element should contain     ${message}     Failed 

Validate success process 
[documentation]        confirmed process is done 
Element should contain     ${message}     Completed successfully

Upvotes: 6

Views: 23913

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

The most common way to do this is to have your python code raise an exception rather than return True or False:

for i in range(timeout):
    if wait_for_failed_proccess is True:
       raise Exception("Process timed out")
    ...
...

With the above, you don't have to do anything in your test -- if this keyword raises an exception, the test will automatically fail.

If you prefer to return a true or false value, you can use the built-in fail keyword:

Run keyword if | ${result}==False | fail | Process timed out

Upvotes: 11

Related Questions