Python 在运行测试套件时动态创建robot框架测试用例

Python 在运行测试套件时动态创建robot框架测试用例,python,robotframework,Python,Robotframework,我有一个非常具体的场景,在这个场景中,我将向数据库插入一些数据(例如,假设有3个插入,每个插入都返回一些ID),并且基于返回值,我希望为这些返回值创建动态测试用例 例如 我试着通过引用来找出如何做到这一点,但没有成功。 您能告诉/建议这是否可行吗?如果可行,我如何实现这一目标。 到目前为止,我发现可能有两种方法可以做到这一点: 通过创建一个侦听器 通过创建自己的关键字 在这两种情况下,我都必须将逻辑放在那里,但我不知道如何动态创建测试用例。 请帮忙?:) 另外,有些例子非常受欢迎。提前感谢有一篇

我有一个非常具体的场景,在这个场景中,我将向数据库插入一些数据(例如,假设有3个插入,每个插入都返回一些ID),并且基于返回值,我希望为这些返回值创建动态测试用例 例如

我试着通过引用来找出如何做到这一点,但没有成功。 您能告诉/建议这是否可行吗?如果可行,我如何实现这一目标。
到目前为止,我发现可能有两种方法可以做到这一点:

  • 通过创建一个侦听器
  • 通过创建自己的关键字
  • 在这两种情况下,我都必须将逻辑放在那里,但我不知道如何动态创建测试用例。
    请帮忙?:)


    另外,有些例子非常受欢迎。提前感谢

    有一篇博文为您提供了答案:

    正如您所建议的,解决方案是创建一个监听器,以便可以动态添加测试。请仔细阅读这篇文章,因为这里有一些限制,比如您可以创建测试,也不能创建测试(在执行过程中)。 另外,这篇文章是针对3.x framework和4.x的,您需要在类中做一个小小的更改,替换: tc.keywords.create(name=kwname,args=args) 与: tc.body.create_关键字(name=kwname,args=args)

    有关如何实现此功能的示例:

    demo.robot:

    *** Settings ***
    Library           DynamicTestCases.py
    
    *** Test Cases ***
    Create Dynamic Test Cases
        @{TestNamesList}    Create List    "Test 1"    "Test 2"    "Test 3"
        FOR    ${element}    IN    @{TestNamesList}
            Add Test Case    ${element}   Keyword To Execute
        END
    
    *** Keywords ***
    Keyword To Execute
        Log    This is executed for each test!
    
    DynamicTestCases.py内容(来自我发布的url的基本副本+更改的行):

    这是一个如何使其工作的小例子。 例如,如果要为关键字指定一个变量,只需添加参数:

    *** Settings ***
    Library           DynamicTestCases.py
    
    *** Test Cases ***
    Create Dynamic Test Cases
        @{TestNamesList}    Create List    "Test 1"    "Test 2"    "Test 3"
        FOR    ${element}    IN    @{TestNamesList}
            Add Test Case    ${element}   Keyword To Execute    ${element}
        END
    
    *** Keywords ***
    Keyword To Execute
        [Arguments]    ${variable}
        Log    The variable sent to the test was: ${variable}
    
    查看此:。请注意,它在Robot Framework 4.0下工作。我将为Robot Framework 4.0更新它。
    from __future__ import print_function
    from robot.running.model import TestSuite
    
    
    class DynamicTestCases(object):
        ROBOT_LISTENER_API_VERSION = 3
        ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
    
        def __init__(self):
            self.ROBOT_LIBRARY_LISTENER = self
            self.current_suite = None
    
        def _start_suite(self, suite, result):
            # save current suite so that we can modify it later
            self.current_suite = suite
    
        def add_test_case(self, name, kwname, *args):
            """Adds a test case to the current suite
    
            'name' is the test case name
            'kwname' is the keyword to call
            '*args' are the arguments to pass to the keyword
    
            Example:
                add_test_case  Example Test Case  
                ...  log  hello, world  WARN
            """
            tc = self.current_suite.tests.create(name=name)
            #tc.keywords.create(name=kwname, args=args) #deprecated in 4.0
            tc.body.create_keyword(name=kwname, args=args)
    
    # To get our class to load, the module needs to have a class
    # with the same name of a module. This makes that happen:
    globals()[__name__] = DynamicTestCases
    
    *** Settings ***
    Library           DynamicTestCases.py
    
    *** Test Cases ***
    Create Dynamic Test Cases
        @{TestNamesList}    Create List    "Test 1"    "Test 2"    "Test 3"
        FOR    ${element}    IN    @{TestNamesList}
            Add Test Case    ${element}   Keyword To Execute    ${element}
        END
    
    *** Keywords ***
    Keyword To Execute
        [Arguments]    ${variable}
        Log    The variable sent to the test was: ${variable}