简体   繁体   中英

Wrapping a test case in Robot Framework in keyword based on the tag of a test

Robot Framework supports two ways to interact with the tests and modify the test structure:

  • the visitor internal API
  • the listener API

I would like to prepend, to specific tests, a given keyword, or even more precisely to wrap the test in a given keyword, if that test has a specific tag. Which of the two approaches should I choose, and how I should use the API of the Body of Robot Framework to wrap it in a Wait Until Keyword Succeeds ?

With a prerunmodifier you can modify the suite before execution, with a listener you can modify the suite during execution. As you have all the information available before execution it is suggested to use a prerunmodifier and do every modifications beforehand.

What needs to be done is creating a user keyword with the same body as the test case that needs to be wrapped. These are the steps:

  1. Check test.tags for your specific tags.
  2. Create a robot.running.model.UserKeyword object for the test case.
  3. Copy test.body to ukw.body effectively replicating the test case as a keyword.
  4. Append the new keyword to the keywords available in the suite's resource ( suite.resource.keywords ). Make sure the keyword name is unique for every test case. (This entire step is that was the missing link in the previous version of the answer.)
  5. Clear the original test.body .
  6. Add the Wait Until Keyword Succeeds that would call the newly made user keyword to test.body .

This will handle IF/ELSE IF/ELSE and FOR blocks or their combinations in nested structures.

start_suite visitor function should be implemented because access is needed to the suite.resource.keywords object.

Visitor.py:

from robot.api import SuiteVisitor
from robot.running.model import UserKeyword

class Visitor(SuiteVisitor):

    def start_suite(self, suite):
        for test in suite.tests:
            if 'WRAP' in test.tags:                                     # step 1.
                keyword_name = f'Wrapper Keyword For Test:{test.name}'

                ukw = UserKeyword(name=keyword_name)                    # step 2.
                ukw.body = test.body                                    # step 3.
                
                suite.resource.keywords.append(ukw)                     # step 4.
            
                test.body.clear()                                       # step 5.
                test.body.create_keyword(                               # step 6.
                    name='Wait Until Keyword Succeeds',
                    args=['10 sec', '2 sec', keyword_name])

Example test and results:

*** Test Cases ***
Test A
    [Tags]    WRAP
    KW
    Log        step 1
    Log Many    step 2    step 3
    IF    True
        Log    true
    END
    FOR    ${i}    IN RANGE    10
        FOR    ${j}    IN RANGE    10
            Log Many    ${i}    ${j}
        END
    END

Test B
    [Tags]    SMOKE
    Log    1
    Log    1


*** Keywords ***
KW
    Log    1
    Log    2

在此处输入图像描述

I think the Listener API might be the way to go but why not use the Test and Suite Setup Keywords to be able to pull off what you are trying to do. However, in general, ie not just in Python or Robot Framework, it is a bad idea to have one test depend on the execution of another test. Ideally, each test should be able to run on its own, whether first or last or anywhere in the sequence so perhaps there might be a need to refactor your tests and/or Keywords to be able to achieve what you are attempting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM