Search code examples
robotframework

is there a way to make robot framework code run a failed testcase multiple times


I will like to run this multiple times to test what happens when users try to login with wrong credentials many times. The problem is that this runs once, and if i try duplicating it again, it doesnt run the rest of the code.

The result is supposed to be ''' HTTP error 401 Unauthorized, Attempt 1 ''' but i will like to see 10 attempts I have tried this but it fails ''' ${result}= Run special command -k --url https://${url} auth login ${username} -p${password} '''


Solution

  • From the same question you most likely presented here https://forum.robotframework.org/t/i-want-to-test-many-attempted-failed-login/4781 - I already suggested to look into Run Keyword And Expect Error

    But apparently that was too cumbersome.

    *** Variables ***
    ${url}    http://localhost
    ${username}   nobody
    ${password}   notmypassword
    
    *** Keywords ***
    Run special command
      [arguments]   ${arg1}   ${arg2}   ${arg3}   ${arg4}   ${arg5}   ${arg6}   ${arg7}
      [return]    "Nothing to see here folks"
      Log To Console     ${arg1} ${arg2} ${arg3} ${arg4} ${arg5} ${arg6} ${arg7}
      Fail    This is the error message for failure
    
    
    *** Test Cases ***
    
    Doesnt Repeat Failure
      Log To Console   \nThis does show because its start of the testcase
      ${result}=    Run special command    -k    --url    https://${url}    auth    login    ${username}    -p${password}
      ${result}=    Run special command    -k    --url    https://${url}    auth    login    ${username}    -p${password}
      ${result}=    Run special command    -k    --url    https://${url}    auth    login    ${username}    -p${password}
      Log To Console   \nThis will not be shown because run special command fails
    
    Does Repeat Failure
    
      Log To Console   \nThis does show because its start of the testcase
    
      ${result}=    Run Keyword And Expect Error  *
      ...   Run special command    -k    --url    https://${url}    auth    login    ${username}    -p${password}
    
      ${result}=    Run Keyword And Expect Error  *
      ...   Run special command    -k    --url    https://${url}    auth    login    ${username}    -p${password}
    
      ${result}=    Run Keyword And Expect Error  *
      ...   Run special command    -k    --url    https://${url}    auth    login    ${username}    -p${password}
    
      Log To Console   \nThis will be shown because we where expecting the error and last call returned a value: : ${result}
    
    Does Repeat Failure with for loop
      Log To Console   \nThis does show because its start of the testcase
      FOR    ${idx}    IN RANGE    10
        Log To Console    \nIteration: ${idx}
        ${result}=    Run Keyword And Expect Error  *
        ...   Run special command    -k    --url    https://${url}    auth    login    ${username}    -p${password}
      END
      Log To Console   \nThis will be shown because we where expecting the error and last call returned a value: : ${result}
    
    

    This example has 3 test cases, one that you are trying to do that will fail if you run it .. 2 other cases that will call your "Run Special command" that always fails but the test itself doesnt fail because the test code EXPECTS that the keyword call will fail with given parameters.