Keywords for RAII like ? // Prepend cleanup to test teardown?

Hi,

I’m currently writing test cases where I change, temporarily, systemd unit and I revert the changes after the test.

So, I have a keyword that set stuffs and a keyword that cleanup those stuffs, like open/close, new/delete, lock/unlock etc.

“Test Teardown” is going to grow and any forgetting/mistake lead to bugs. This sounds like I need solutions like RAII.

In another words, I would like to register “close/delete/unlock” actions from the keyword “open/new/lock”. I guess it’s close to “Register Keyword To Run On Failure” from Selenium library. Unfortunately, I would like something like “Register Keyword To Run On Teardown”.

Do you have any tips to ease cleanup/teardown phase?

Thanks for the help

I’m not sure did I understand this correctly, but if I did something like this could work:

*** Settings ***
Test Teardown    Run Keywords    @{TEARDOWN KEYWORDS}

*** Variable ***
@{TEARDOWN KEYWORDS}

*** Test Cases ***
Example
    Register Keyword To Run On Teardown    No Operation
    Register Keyword To Run On Teardown    Keyword

*** Keywords ***
Register Keyword To Run On Teardown
    [Arguments]    ${name}
    Set Test Variable    @{TEARDOWN KEYWORDS}    @{TEARDOWN KEYWORDS}    ${name}

Keyword
    No Operation
1 Like