Skip to main content
 首页 » 编程设计

python之__init__.txt 中定义的关键字不会被提取用于测试设置和测试拆卸

2025年01月19日10zengkefu

我想尝试用户定义的关键字作为机器人框架测试套件和子测试套件中测试用例的设置和拆卸。我有以下结构,

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

__init__.txt 包含以下文本

 
 
    *** Settings *** 
    Test Setup    My Keyword1 
    Test Teardown    My Keyword2 
 
    *** Keywords *** 
    My Keyword1 
        Log    Inside My Keyword1 
 
    My Keyword2 
        Log    Inside My Keyword2 

Sample.robot 包含以下代码,

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

运行 robot tests 给我以下错误,

 
 
    ============================================================================== 
    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 
    ============================================================================== 
 

你能告诉我在上面的结构中我遗漏了什么吗?我需要一种机制,允许我执行用户关键字作为默认设置或拆卸。此外,如果需要,单个测试用例可以覆盖设置/拆卸。

请您参考如下方法:

问题是 My Keyword1 和 My Keyword2 在 __init__.txt 文件中是本地的,不能在其他测试用例中使用。您需要将它们移动到资源文件中,并将该文件导入到您的测试中。

来自 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.