如何在机器人框架中设置多级测试setup/teardown

How to set multi-level test setup/teardown in robot framework

我有一些机器人测试用例在目录中分开。目录层次结构为:

ParentTestDirectory
    |__ ChidTestDirectoryOne
        |__ TestOne.robot
    |__ ChidTestDirectoryTwo
        |__ TestTwo.robot
    |__ __init__.robot

__init__.robot的内容:

*** Settings ***
Test Setup          LOG TO CONSOLE   Test setup from __init__.robot
Test Teardown       LOG TO CONSOLE   Test teardown from __init__.robot

TestOne.robot的内容:

*** Settings ***
Test Setup          LOG TO CONSOLE   Test setup from TestOne.robot
Test Teardown       LOG TO CONSOLE   Test teardown from TestOne.robot
*** Test Cases ***
Test One
    LOG TO CONSOLE   This is Test One!

TestTwo.robot的内容:

*** Settings ***
Test Setup          LOG TO CONSOLE   Test setup from TestTwo.robot
Test Teardown       LOG TO CONSOLE   Test teardown from TestTwo.robot
*** Test Cases ***
Test Two
    LOG TO CONSOLE   This is Test Two!

我有一个用 python 编写的 运行ner,它使用机器人 运行ner 模块;这是使用命令 sudo python run.py --testsuit scenarios.ParentTestDirectory:

运行ning 测试用例的结果
==============================================================================
Scenarios                                                                     
==============================================================================
Scenarios.ParentTestDirectory                                                 
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryOne                            
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryOne.TestOne                    
==============================================================================
Test One                                                              Test setup from TestOne.robot
.This is Test One!
.Test teardown from TestOne.robot
Test One                                                              | PASS |
------------------------------------------------------------------------------
Scenarios.ParentTestDirectory.ChidTestDirectoryOne.TestOne            | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryOne                    | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo                            
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo.TestTwo                    
==============================================================================
Test Two                                                              Test setup from TestTwo.robot
.This is Test Two!
.Test teardown from TestTwo.robot
Test Two                                                              | PASS |
------------------------------------------------------------------------------
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo.TestTwo            | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory.ChidTestDirectoryTwo                    | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Scenarios.ParentTestDirectory                                         | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Scenarios                                                             | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================

如您所见,它只是 运行 最新的测试 setup/teardown。我也希望它从父目录 运行 测试 setups/teardowns 并且它应该在子目录之前执行。换句话说,我希望每个测试用例的父设置在自己设置之前分别为 运行。我可以使用机器人框架功能实现这一目标吗?

一个测试用例只能有一个设置。当您将 Test Setup 放入整个套件的设置中时,即定义了 default 测试设置。如果 child 套件或单个测试定义了测试设置,它将 运行 而不是 更高层定义的套件级别设置。

在机器人框架用户指南中标题为 Initialization files 的部分中,它是这样说的(强调是我添加的):

Test Setup, Test Teardown, Test Timeout

Set the default value for test setup/teardown or test timeout to all test cases this directory contains. Can be overridden on lower level. Support for defining test timeout in initialization files was added in Robot Framework 2.7.

如果您希望套件定义 运行 除了每个测试指定的设置之外的测试设置,您应该将该代码放入自定义关键字中,并让每个测试调用该关键字作为一部分它的设置。