Search code examples
robotframeworkteardown

What's wrong with my robot framework teardown?


I'm new to using robot framework and I'm struggling to get my teardown to work. It currently looks like:

[Teardown]  run keyword if any tests failed  KeyFail

When I run the program with code like this, I get the error: Keyword 'Run Keyword If Any Tests Failed' can only be used in suite teardown.

I can change it so that I put it inside it's own test case, however I then get the error that: Test Case contains no keywords.

Please advise me as to what I'm doing wrong. It would be appreciated. Thanks.

Edit:

***Keywords***
Generation
    (Some stuff)
KeyFail
    log to console  Error report being sent.


***Test Cases***
Requires successful generation of file
    Generation
Teardown Case
    [Teardown]  run keyword if any tests failed  KeyFail

Edit: And how to fix this problem. Thanks


Solution

  • It looks like you have defined it in the test case teardown instead of the test suite teardown. You can change it to use the Test teardown instead.

    Edit: Here are two solutions:
    1. Change your keyword to the TEST specific one, Run Keyword If Test Failed which applies to the last executed test, and can only be used in a test teardown.
    2. The second is to use Suite Setups / teardowns. These apply to ALL test cases that you run. Like this:

    ***Settings***
    Suite Setup    Your Test Setup Keyword
    Suite Teardown  run keyword if any tests failed  KeyFail
    
    ***Keywords***
    Generation
        (Some stuff)
    KeyFail
        log to console  Error report being sent.
    
    
    ***Test Cases***
    Requires successful generation of file
        Generation
    Teardown Case
        Stuff to do
        # teardown is automatic, and does not need to be called.