2

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.

0

2 Answers 2

2

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.

1
  • Thanks. Does that mean, there is no use of creating new keywords (or keywords table) in the initialization files as initialization files are not explicitly imported? Secondly, I am not sure whether it is just me or others will stumble to this issue as well. Since I hadn't explicitly used the created keywords in sample.robot, I didn't feel the need to import the related resource file in sample.robot. Perhaps a quick example in the documentation will make things clearer? Apr 11, 2018 at 5:53
0

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.

4
  • No. I need the setup and teardown to be repeated for each test case and not once per suite. Apr 10, 2018 at 15:53
  • 1
    Then you should not use the __init__. Use the "Test setup" and "Test Teardown" inside your sample.robot file.
    – hfc
    Apr 11, 2018 at 8:43
  • Yes, makes sense. Also thanks for the .robot extension tip for __init__. Apr 11, 2018 at 10:40
  • You can actually use Test setup and Test teardown in the __init__.txt file and they will be run before and after each test in each suite in that directory -- unless you overwrite them in the sample.robot file. Apr 12, 2018 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.