Robotframework: setup/teardown for every test run in a templated test

Created on 1 Sep 2017  路  3Comments  路  Source: robotframework/robotframework

Something that I've missd many times now for using robot test in a templated and more of a TDD manner is a teardown keyword that allows me to define which keyword to run whenever one "iteration" of a templated test is run, setup respectively.
I'm dealing with services and networking mocks that need a clear state whenever a test starts. If one test iteration fails, the state will not be "fresh" without a teardown followed by a setup.

Most helpful comment

When you use templates and want run multiple iterations per test, then the keyword used as a template needs to take care of possible iteration specific setup/teardown activities. With keywords implemented in Python you'd use something like try/finally or a with statement to ensure clean-up activities are always executed at the end. With higher level keywords defined in RF data you can use [Teardown]. In your case it could look like this:

*** Keywords ***
Test Iteration KW
    [Arguments]   ${var}
    Iteration Setup KW
    Log To Console      In Test case: ${var}
    [Teardown]    Iteration Teardown KW

All 3 comments

Did you try using the Test Setup and Test Teardown in conjunction with the Test Template in the *** Settings *** section of a robot test suite?

http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#test-setup-and-teardown

Here is a sample test suite

*** Settings ***
Test Template   Test KW
Test Setup      Test Setup KW
Test Teardown   Test Teardown KW

*** Test Cases ***
Test1       1
Test2       2
Test3       3

*** Keywords ***

Test Setup KW
    Log To Console    In Test Setup

Test Teardown KW
    log to console    In Test Teardown


Test KW
    [Arguments]   ${var}
    Log To Console      In Test case: ${var}

with the output being

==============================================================================
Foo                                                                           
==============================================================================
Test1                                                                 In Test Setup
.In Test case: 1
.In Test Teardown
Test1                                                                 | PASS |
------------------------------------------------------------------------------
Test2                                                                 In Test Setup
.In Test case: 2
.In Test Teardown
Test2                                                                 | PASS |
------------------------------------------------------------------------------
Test3                                                                 In Test Setup
.In Test case: 3
.In Test Teardown
Test3                                                                 | PASS |
------------------------------------------------------------------------------
Foo                                                                   | PASS |
3 critical tests, 3 passed, 0 failed
3 tests total, 3 passed, 0 failed
==============================================================================
Output:  /Users/bassam.khouri/Documents/git/autotest/results/output.xml
Log:     /Users/bassam.khouri/Documents/git/autotest/results/log.html
Report:  /Users/bassam.khouri/Documents/git/autotest/results/report.html

You can even override the default Test Teardown for a single iteration. Here's a sample

*** Settings ***
Test Template   Test KW
Test Setup      Test Setup KW
Test Teardown   Test Teardown KW

*** Test Cases ***
Test1       1
Test2       2
    [Teardown]  Test Teardown Alt KW
Test3       3

*** Keywords ***

Test Setup KW
    Log To Console    In Test Setup

Test Teardown KW
    log to console    In Test Teardown

Test Teardown Alt KW
    log to console    In Alternate Test Teardown

Test KW
    [Arguments]   ${var}
    Log To Console      In Test case: ${var}

Is this what you are looking for?

Hi @bkhouri, thanks for this very interesting reply.

This indeed works and does what I want, so it helps :)

However, what I was trying, and where the _Test Teardown_ lacked, was

```* Settings *
Test Setup Test Setup KW
Test Teardown Test Teardown KW

* Test Cases *
Run templated Keyword
[Template] Test KW
1
2
3

In this case, the templated test cases run, but there's only one setup/teardown:

Run templated Keyword In Test Setup
.In Test case: 1
.In Test case: 2
.In Test case: 3
.In Test Teardown

Run templated Keyword | PASS |

Satellite Log | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed

```

I wasn't clear to me from the docs that this would make a difference. The reason for why I chose a non-global template definition was for flexibility: For templating diffierent test cases I won't need a file for each.

When you use templates and want run multiple iterations per test, then the keyword used as a template needs to take care of possible iteration specific setup/teardown activities. With keywords implemented in Python you'd use something like try/finally or a with statement to ensure clean-up activities are always executed at the end. With higher level keywords defined in RF data you can use [Teardown]. In your case it could look like this:

*** Keywords ***
Test Iteration KW
    [Arguments]   ${var}
    Iteration Setup KW
    Log To Console      In Test case: ${var}
    [Teardown]    Iteration Teardown KW
Was this page helpful?
0 / 5 - 0 ratings