Skip to content

Fail keyword causes assert on Test Teardown #2358

Closed

Description

@GLMeece

Summary

For a given test case, you may call the Fail keyword at any time, but you cannot call it during the Test Teardown phase.

Detail

  • If Fail is called as the last step in a test case, setting the case status (including any msg you supply) works fine.
  • However, if you attempt to call this keyword during the test teardown phase, you will get the following if you don't include a message: Teardown failed: AssertionError
  • If you do include a message, the HTML tag isn't honored:
Teardown failed:
*HTML* <b>My Message Title</b>
<span style="color:red;font-weight:bold">φ FAIL</span>

... etc.

Here is an example of a keyword that does work if it is called as the last step in a test case, and should work if called as a Test Teardown:

Finish Test Messages
    [Documentation]     This runs fine if called inline with a test case,
    ...                 however, it will fail if called from Test Teardown
    Run Keyword If  ${test_fail_count} > 0   Fail  msg=*HTML* ${test_messages}
    ...     ELSE    Set Test Message    *HTML* ${test_messages}

Suggested Fix Approaches

  1. Don't allow this keyword to throw an assertion merely because it is called in the test teardown phase.
  2. If the above is not acceptable, update the documentation to reflect that it cannot be called during any teardown phase (test or suite).

Activity

  1. pekkaklarck commented on Apr 27, 2016

    @pekkaklarck
    Member

    My guess is that using *HTML* doesn't work because the actual error is prefixed with Teardown failed:. The same problem would occur also with continuable failures if there are more than one failure. I'm not sure how easy this is to fix but I can take a look. Can you clarify is there some other issue than this?

  2. GLMeece commented on Apr 27, 2016

    @GLMeece
    Author

    @pekkaklarck - I'm assuming that as well. I guess my point is - why is teardown failing? Unless I flag the test case as failed, it appears to pass and the HTML is rendered correctly. Only if I mark it as failed (per my keyword example, if the ${test_fail_count} is greater than zero) will it blow up.

    My point is this - if you have the Fail built-in keyword executing during test teardown, it says that teardown failed. That was an unexpected behavior to me. I would have thought that teardown failing would have meant that whatever action(s) I had specified for teardown couldn't be completed. Is the Fail keyword out of scope at that point? Otherwise, it seems that it should work on test case teardown (I would understand if I tried to do this at Suite teardown).

    Let me know if my reply doesn't make sense. 😕

  3. GLMeece commented on Apr 27, 2016

    @GLMeece
    Author

    To further illustrate...

    This Works!

    (nothing declared for Test Teardown):

    *** Test Cases ***
    API - Lender Management Routes
        [Documentation]     = Case Steps =
        ...
        ...             | *Step* | *Verification Action* |
        ...             | _1._ | Retrieve Package Label File response is good. |
        ...             | _2._ | Get Address Validation response is good. |
        ...             | _3._ | Evaluate pass/fail within test case. |
        [Tags]  positive    API    get   smoke
        Given Get Package Label File Response Is Good
        And Get Package Address Validate Is Good
        Then Finish Test Messages

    This Throws the Assert

    *** Settings ***
    
    Test Teardown       Finish Test Messages
    
    *** Test Cases ***
    API - Lender Management Routes
        [Documentation]     = Case Steps =
        ...
        ...             | *Step* | *Verification Action* |
        ...             | _1._ | Retrieve Package Label File response is good. |
        ...             | _2._ | Get Address Validation response is good. |
        [Tags]  positive    API    get   smoke
        Given Get Package Label File Response Is Good
        Then Get Package Address Validate Is Good

    @pekkaklarck - Is that any more helpful? 🔍

  4. pekkaklarck commented on Apr 27, 2016

    @pekkaklarck
    Member

    How Finish Test Messages behaves depends on ${test_fail_count}. Without a full executable example I cannot really comment is the behavior correct or not.

    Notice that Fail doesn't do anything more than raise AssertionError(msg) and thus isn't any different to any other keyword that could potentially fail in teardown.

    Notice also that you could simplify Finish Test Messages like this:

    *** Keywords ***
    Finish Test Messages
        Should Be True    ${test_fail_count} == 0    msg=*HTML* ${test_messages}
        Set Test Message    *HTML* ${test_messages}
  5. GLMeece commented on Apr 27, 2016

    @GLMeece
    Author

    When ${test_fail_count} is zero, the test message is set and formatted just fine. Even if I strip all of that out and just do this at Test Teardown:

    Finish Test Messages
        Fail

    then I still get the Teardown failed: AssertionError in the Message column of the report. If I don't call Fail then no assertion happens. Again, it only happens if it is called during teardown - it doesn't happen if it is called as one of the steps (logically, the last) of the given test case.

    So - is this by design? If so, then the documentation for this keyword should include a note: "If this keyword is executed during Test Teardown, an assert will be output to the test message."

    If this was not by design... 🪲

    IMO, it would be preferable for this to be usable during teardown such that the test case author could determine whether the case passed or failed.

  6. pekkaklarck commented on Apr 27, 2016

    @pekkaklarck
    Member

    Fail always fails with message AssertionError if you use it without an explicit message. In teardown the only difference is that you get that additional Teardown failed: prefix.

  7. GLMeece commented on Apr 27, 2016

    @GLMeece
    Author

    OK, @pekkaklarck - I've probably wasted enough of your time on this. I appreciate it!

    So if I understand you correctly, if I want to do my own eval and set Fail on a test case, I need to do it before teardown or I'll get the "teardown failed" message, along with it ignoring the *HTML* formatting "switch" - yes?

    If so - I understand, even though it's kind of a bummer that I must have one additional step in each and every test case to do this vs. just one time in the Test Teardown. 😢

  8. pekkaklarck commented on Apr 28, 2016

    @pekkaklarck
    Member

    I think Teardown failed is good but it breaking HTML is definitely bad. A separate bug should be submitted about it.

  9. GLMeece commented on Apr 28, 2016

    @GLMeece
    Author

    @pekkaklarck - hopefully issue 2360 gives you what you need. Let me know if it does not.

  10. pekkaklarck commented on May 31, 2016

    @pekkaklarck
    Member

    Closing in favor of #2360.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions