在Python中动态创建机器人关键字

问题描述 投票:0回答:1

我有一个实现机器人框架关键字的Python库。 我正在尝试根据给定的 yaml 文件自动生成测试套件,但无法执行其中一个步骤。

这是简化的代码:

from robot.api import TestSuite
from robot.api.deco import keyword
import yaml

@keyword("Request Api Call")
def request_api_call(request_type, api_url, query_params, headers, body, expected_status):
    # Does the API call

@keyword("Generate Robot Tests")
def generate_robot_tests(test_cases, file_path):
    suite = TestSuite(suite_name)
    for test_case in test_cases:
        test = suite.tests.create(test_case_name)
        test.body.create_keyword('Request Api Call', args=[request_type_value, api_url_value, query_params_value, headers_value, body_value, expected_status_value])
    return suite

问题出在

test.body.create_keyword
。它无法识别
Request Api Call
关键字。

当我直接从导入此 python 文件的 .robot 文件调用该关键字时,它可以工作。 我猜这是某种范围问题,但不确定如何解决。

我想要完成的是单独运行每个测试(以获取单独的日志和测试结果),因此我认为我可以为每个 yaml 文件创建一个测试套件,然后为该 yaml 文件中的每个条目创建一个测试用例。

有什么想法吗?谢谢!

python robotframework robot
1个回答
0
投票

您已使用 robots.api 模块创建了 Robot Framework 测试套件,并添加了带有自定义设置步骤的测试。通常,如果您执行的关键字已经存在,则不会遇到此问题。如果您想创建自己的自定义关键字或使用不同库中的关键字,则需要确保将相应的库导入到您的套件中。

导入自定义库

suite.resource.imports.library('path.to.your.CustomLibrary')

© www.soinside.com 2019 - 2024. All rights reserved.