首页 > 解决方案 > 对嵌套文件夹结构机器人框架中的多个测试套件使用通用套件设置和套件拆解

问题描述

我是Robot框架的新手,当测试的文件夹结构不是“扁平”时,我试图更好地理解Suite SetupSuite Teardown中的概念和用法。在网上搜索了很久之后,Robot 框架用户指南 - 执行测试部分,还有这个与我的情况类似但不完全一样的问题,我仍然没有找到任何解决方案,所以我们开始吧。

我的项目现在包含以下文件:

_init__.robot其中包含Suite Setup& Suite Teardown“定义”,如下:

*** Settings ***
Library   /path/to/some/python/file.py
Suite Setup  myCustomSuiteSetup
Suite Teardown  myCustomSuiteTeardown

*** Keywords ***
myCustomSuiteSetup
    ${ret_code} =  run keyword MySuiteSetupKeyword
    should be eqaul as integers ${ret_code} 0

myCustomSuiteTeardown
    ${ret_code} =  run keyword MySuiteTeardownKeyword
    should be eqaul as integers ${ret_code} 0

wheremyCustomSuiteTeardownMySuiteTeardownKeywordare 关键字“链接”到文件中的某些 Python 函数/path/to/some/python/file.py

我项目中的 4 个套件文件目前排列如下:

|--tests
|----suite_1.robot
|----suite_2.robot 
|----suite_3.robot
|----suite_4.robot
|----__init__.robot

Suite Setup现在, &的目的(和用法)Suite Teardown是,将在整个文件夹Suite Setup的运行开始时运行,即,在第一个套件的第一个测试用例之前,在这种情况下是,并且将最后一个套件的最后一个测试用例,在这种情况下是.testssuite_1.robotSuite Teardownsuite_4.robot

为此,我只需按如下方式调用所有套件(从文件夹“上方”的一个文件tests夹中):

robot tests

到目前为止,一切都很好。

现在我的问题如下:实际上我希望“重新排列”测试文件的文件夹结构,如下所示:

|--tests
|----testGroup1
|--------suite_1.robot
|--------suite_2.robot 
|----testGroup2
|--------suite_3.robot
|--------suite_4.robot
|--__init__.robot    <----- Where the __init__.robot file should be placed now ?

意思是,将测试套件“收集”到子文件夹,但是,我仍然希望像以前一样保持Suite Setup&的使用Suite Teardown,即在调用“根”文件夹下的每个可能的测试套件子集时testsSuite Setup&Suite Teardown必须是要执行的第一个和最后一个(分别)“步骤”,例如,假设我希望运行suite_3.robot& suite_4.robot,那么现在,Suite Setup应该在第一个测试用例之前调用,suite_3.robot并且Suite Teardown应该在最后一个测试用例之后调用suite_4.robot. 另外,当然,我希望只保留文件的一个副本__init__.robot- 即 - 不要__init__.robot在每个子文件夹中保留两个相似的副本,testGroup1&testGroup2. 当我这样做时,它起作用了,但这不是我希望这样做的(正确)方式。

所以我的问题是:

  1. 我需要将__init__.robot文件放在哪里?

  2. 例如,如果我希望只运行testGroup2(ie- suite_3.robot& suite_4.robot) 中的两个测试套件,我需要使用什么命令?

当然,如果这不是实现我的目标的“正确”方式(方法)(单个和统一Suite Setup以及Suite Teardown每个测试套件子集) - 请建议应该如何完成。

注意:我使用的是 Robot framework 3.1.2(Linux 上的 Python 3.5.2)

标签: pythonpython-3.xautomationautomated-testsrobotframework

解决方案


tests您在包含两者的文件夹中testGroup1放置的初始化文件testGroup2是正确的。tests这样,如果您在其子文件夹中的某个位置或内部运行至少一个测试,则套件安装程序将在所有此类测试之前运行,然后在套件拆解之后运行。

要强制运行 Suite Setup 和 Suite Teardown,您必须将父文件夹 ( tests) 作为您运行的文件夹。最好始终运行包含所有测试的文件夹。这样,您就不会错误地错过任何套件设置或拆解。

然后运行选择测试,使用选项--include--exclude标记,--suite--test

在您的示例中,您将运行以下命令:

robot --suite suite_3 --suite suite_4 tests, 仅运行 suite_3 和 suite_4

或者,您可以使用完全限定的套件名称:

robot --suite tests.testGroup2.suite_3 --suite tests.testGroup2.suite_4 tests


推荐阅读