Skip to content

setup/teardown for every test run in a templated test #2668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sebkraemer opened this issue Sep 1, 2017 · 8 comments
Closed

setup/teardown for every test run in a templated test #2668

sebkraemer opened this issue Sep 1, 2017 · 8 comments

Comments

@sebkraemer
Copy link

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.

@bkhouri
Copy link

bkhouri commented Sep 5, 2017

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?

@sebkraemer
Copy link
Author

sebkraemer commented Sep 8, 2017

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

Test Setup      Test Setup KW
Test Teardown   Test Teardown KW

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

<keyword section identical>

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.

@pekkaklarck
Copy link
Member

pekkaklarck commented Sep 10, 2017

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

@jpl-pix4d
Copy link

I would also be interested to have the global setup and teardown routines, defined in the *** Settings *** section, run after each iteration of the template. Is this place the good place to ask for an update or request a feature? 🤔

@pekkaklarck
Copy link
Member

Could you clarify why that would be beneficial compared to what's already possible by using keyword teardown?

@jpl-pix4d
Copy link

Hello
Thank you for getting back.

Current status
I have very standard setup and teardown routines.
The setup routine 1) starts the application under test, 2) creates a temp working directory.
The teardown routine does the opposite i.e. 1) shuts down the application, 2) delete the tmp folder.

I usually have a mix of individual tests with templated tests in the same Robot file. The setup and teardown routines are defined globally for the individual tests. They should also be called for each sub-test of the templated tests. In order to achieve this, I do the following:
The extra work consists of:

  • undefining the global setup and teardown routines at the beginning and end of the templated test
  • redefine the setup routine at the beginning of the templated test
  • redefine the teardown routine at the end of the templated test
    Cf picture.

image

Potential benefit
I wish I could save this extra work and these routines would be called before and after each sub-test of templated tests. In the picture, you can see the desired syntax.
image

@pekkaklarck
Copy link
Member

pekkaklarck commented Apr 30, 2022

I prefer test setup/teardown being executed once per test. Even if you were able to convince me that they should be run for each templated iteration, changing the behavior would so badly backwards compatibility that it couldn't realistically be done.

The only possible solution would be adding some new setting to control this but I seriously doubt that's worth the effort.

@pekkaklarck
Copy link
Member

Not sure why I reopened this in 2022, probably by mistake. Anyway, now that user keywords can even have an explicit setup (#4747) I see no reason to add any new functionality to specify setup/teardown for iterations.

@pekkaklarck pekkaklarck closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants