如何在机器人框架中设置多级测试设置/拆卸 [英] How to set multi-level test setup/teardown in robot framework
问题描述
我在目录中分开了一些机器人测试用例.目录层次结构为:
I have some robot test cases separated in directories. The directory hierarchy is:
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编写的运行程序,它使用机器人运行程序模块;这是使用命令sudo python run.py --testsuit scenarios.ParentTestDirectory
运行测试用例的结果:
I have a runner written in python which uses robot runner module; this is the result of running test cases with command sudo python run.py --testsuit scenarios.ParentTestDirectory
:
==============================================================================
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
==============================================================================
如您所见,它仅运行最新的测试设置/拆卸.我希望它也从父目录运行测试设置/拆解,并且应该在子目录之前执行.换句话说,我希望在其自己的设置之前为每个测试用例分别运行父设置.我可以通过机器人框架功能实现这一目标吗?
As you see, it just runs the latest test setup/teardown. I want it to run test setups/teardowns from parent directories too and it should be executed prior to child's. In other words I want the parent setup to be run for each test case separately before it's own setup. Can I achieve this with robot framework capabilities?
推荐答案
一个测试用例只能有一个设置.将Test Setup
放在整个套件的设置中时,即定义了 default 测试设置.如果子套件或单个测试定义了测试设置,则会在定义更高级别的套件级别设置的 上运行.
A test case can only have a single setup. When you put Test Setup
in the settings for a suite as a whole, that defines a default test setup. If a child suite or an individual test defines a test setup, it will be run instead of the suite level setup defined higher up.
在机器人框架用户中标题为初始化文件的部分中指南,它说了这一点(我特别强调):
In the section titled Initialization files in the robot framework user guide, it says this (with emphasis added by me):
测试设置,测试拆卸,测试超时
为该目录包含的所有测试用例设置默认值,以进行测试设置/拆卸或测试超时. 可以在较低级别上覆盖.在Robot Framework 2.7中添加了对在初始化文件中定义测试超时的支持.
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.
如果您希望套件定义除每个测试指定的设置之外还运行的测试设置,则应将该代码放入自定义关键字中,并让每个测试将其作为该设置的一部分来调用该关键字.
If you want the suite to define a test setup that is run in addition to the setup specified by each test, you should put that code into a custom keyword and let each test call that keyword as part of its setup.
这篇关于如何在机器人框架中设置多级测试设置/拆卸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!