How to make multi-lines test setup or teardown in RobotFramework without creating new keyword?

37,977

Solution 1

Use the "Run Keywords" keyword.

From doc "This keyword is mainly useful in setups and teardowns when they need to take care of multiple actions and creating a new higher level user keyword would be an overkill"

Would look like that:

Test Case
  [Teardown]  Run Keywords  Teardown 1  Teardown 2

or also

Test Case
  [Teardown]  Run Keywords  Teardown 1  
  ...                       Teardown 2 

and with arguments

Test Case
  [Teardown]  Run Keywords  Teardown 1  arg1  arg2
  ...         AND           Teardown 2  arg1  

Solution 2

For executing multiple keywords in Test Teardown method use the following trick:

Firstly, define a new keyword containing the set of keywords you want to execute.

E.g. here Failed Case Handle is a new definition of the other two keywords take screenshot and close application. Consider this is to take a screenshot and then close the running application.

*** Keywords ***
Failed Case Handle
    take screenshot
    close application

Basically, when you call the Failed Case Handle keyword, take screenshot and close application will be executed respectively.

Then, in the ***Settings*** section define the Test Teardown procedure by the following example.

*** Settings ***
Test Teardown  run keyword if test failed  Failed Case Handle

or,

*** Settings ***
Test Teardown  run keyword  Failed Case Handle

So, in the first case Failed Case Handle keyword will be called if any test case fails. On the other-hand in the second case Failed Case Handle keyword will be called after each test cases.

Share:
37,977

Related videos on Youtube

Kirill
Author by

Kirill

Updated on August 09, 2020

Comments

  • Kirill
    Kirill over 3 years

    I need to call two teardown keywords in test case but must not create new keyword for that. I am interesting if there is a such syntax for keywords as for documentation or loops for example:

    [Documentation]  line1
    ...              line2
    ...              line3
    
  • Kirill
    Kirill about 10 years
    In this case Teardown 2 is argument for Teardown 1 so it doesn't work.
  • Laurent Bristiel
    Laurent Bristiel about 10 years
    oh yes, try with "run keywords" then
  • Kirill
    Kirill about 10 years
    Thanks. It almost helps me. There is one problem - it doesn't work if I have to provide argument for Teardown 1 or 2.
  • Laurent Bristiel
    Laurent Bristiel about 10 years
    from the keyword doc: "Starting from Robot Framework 2.7.6, keywords can also be run with arguments using upper case AND as a separator between keywords. The keywords are executed so that the first argument is the first keyword and proceeding arguments until the first AND are arguments to it. First argument after the first AND is the second keyword and proceeding arguments until the next AND are its arguments. And so on"
  • Kirill
    Kirill about 10 years
    Awesome! Got it. Thanks a lot!
  • Kirill
    Kirill about 10 years
    By the way there is a way to not pass Teardown in Settings if I have already another in Test Case?
  • Laurent Bristiel
    Laurent Bristiel about 10 years
    test case "teardown" is overriding test suite "test teardown" defined at the suite level. See in the User Guide: "The easiest way to specify a setup or a teardown for test cases in a test case file is using the Test Setup and Test Teardown settings in the Setting table. Individual test cases can also have their own setup or teardown. They are defined with the [Setup] or [Teardown] settings in the test case table and they override possible Test Setup and Test Teardown settings." robotframework.googlecode.com/hg/doc/userguide/…
  • Laurent Bristiel
    Laurent Bristiel over 7 years
    @SlavaSemushin the new link is robotframework.org/robotframework/latest/…