2
votes

I want to try out user defined keywords as setup and teardown for test cases in a Robot Framework test suite and sub-test suites. I have following structure,

    ROOT
    |
    |- tests
        |- __init__.txt
        |- sample.robot

__init__.txt contains the following text


    *** Settings ***
    Test Setup    My Keyword1
    Test Teardown    My Keyword2

    *** Keywords ***
    My Keyword1
        Log    Inside My Keyword1

    My Keyword2
        Log    Inside My Keyword2

Sample.robot contains following code,


    *** Test Cases ***
    My Testcase1
        Log    Inside My Testcase1

Running robot tests gives me the following error,


    ==============================================================================
    Tests
    ==============================================================================
    Tests.Sample
    ==============================================================================
    My Testcase1                                                          | FAIL |
    Setup failed:
    No keyword with name 'My Keyword1' found.

    Also teardown failed:
    No keyword with name 'My Keyword2' found.
    ------------------------------------------------------------------------------
    Tests.Sample                                                          | FAIL |
    1 critical test, 0 passed, 1 failed
    1 test total, 0 passed, 1 failed
    ==============================================================================
    Tests                                                                 | FAIL |
    1 critical test, 0 passed, 1 failed
    1 test total, 0 passed, 1 failed
    ==============================================================================

Can you please let me know what am I missing in the above structure? I need a mechanism which will allow me to execute a user keyword as setup or teardown for default. Also if required an individual test case can override the setup/teardown.

2

2 Answers

1
votes

The problem is that My Keyword1 and My Keyword2 are local to the __init__.txt file and cannot be used in other test cases. You will need to move them into a resource file, and import that file into your test.

From the robot framework user guide section on initialization files:

Variables and keywords created or imported in initialization files are not available in the lower level test suites. If you need to share variables or keywords, you can put them into resource files that can be imported both by initialization and test case files.

0
votes

Do not use "Test setup" and "Test Teardown" in the Settings section of your __init__ file. Instead use "Suite Setup" and "Suite Teardown". This is because your are going to execute this setup for all the test suites within this folder.

Also, it is a good idea to change the extension of the __init__ file to .robot instead of .txt.