[英]How to "run a keyword if tag="debug"" in suite setup in robot framework
I am new to Robot Framework.我是机器人框架的新手。 I have a .robot file which has 5 test cases where i have defined tags fro each of them.
我有一个 .robot 文件,其中有 5 个测试用例,我为每个测试用例定义了标签。 ie 3 of the test cases has
[tags] debug
and two of the test cases has [tags] preprod
即 3 个测试用例有
[tags] debug
,两个测试用例有[tags] preprod
Now i have a suite setup and suite teardown where for those test cases which has tags debug will perform certain steps and for tag preprod doesnt not require same steps to be performed现在我有一个套件设置和套件拆卸,其中对于那些具有标签调试的测试用例将执行某些步骤,而对于标签预生产不需要执行相同的步骤
For Ex :例如:
*** Settings ***
Suite Setup Run Keywords
... Connect To DB AND
... Create An Employee
Suite Teardown Run Keywords
... Delete DB entries AND
... Disconnect From DB
*** Test Cases ***
TC1
[Tags] Debug
log to console Test1
TC2
[Tags] Debug
log to console Test2
TC3
[Tags] Debug
log to console Test3
TC4
[Tags] preprod
log to console Test4
TC5
[Tags] preprod
log to console Test4
Now TC4 and TC5 does not require to perform Create An Employee
in suite setup and Delete DB entries
in suite teardown现在 TC4 和 TC5 不需要在套件设置中执行
Create An Employee
和在套件拆卸中Delete DB entries
How to implement if the test case has a tag=Debug
perform steps in suite setup and suite teardown如何实现如果测试用例有
tag=Debug
执行套件设置和套件拆卸中的步骤
The suite setup is run once before any of the tests start.套件设置在任何测试开始之前运行一次。 It is impossible to have it do something different for each test.
不可能让它为每个测试做不同的事情。
If you want to perform some action for each test, you need to use a test setup .如果要为每个测试执行某些操作,则需要使用测试设置。 For example, here is a keyword that will log different strings if the test has a "Debug" or "preprod" tag:
例如,如果测试具有“Debug”或“preprod”标签,则这里有一个关键字将记录不同的字符串:
*** Settings ***
Test Setup Perform logging
*** Keywords ***
Perform logging
run keyword if 'Debug' in $test_tags
... log this test has a debug tag
run keyword if 'preprod' in $test_tags
... log this is preprod WARN
In continuation with Bryan answer.继续布莱恩的回答。 You need to understand that a Test Setup defined in settings section will be applicable to all the test cases.
您需要了解设置部分中定义的测试设置将适用于所有测试用例。
as a result for both Debug and preprod tags , you are getting the same Test setup executed.因此,对于Debug和preprod标签,您将执行相同的测试设置。
In short, you want different test set up based on Tags.简而言之,您需要基于标签设置不同的测试。
To achieve this you need to use custom keywords of same name ie Test Setup in the test cases where you intend to have specific tags.为了实现这一点,您需要在您打算使用特定标签的测试用例中使用同名的自定义关键字,即测试设置。
Here is an example to do the same.这是一个执行相同操作的示例。
*** Settings ***
Test Setup log to console parent test setup
*** Test Cases ***
TC1
[Tags] Debug
log to console Test1
${success} = Run Keyword and Return Status Submit The Job #Code will not switch to next keyword untill this keyword return the satus as True or False
log ${success}
wait until keyword succeeds 2x 200ms Submit The Job #This Kw will run the KW 2 times if KW fails while waiting 200ms after each result
TC2
[Tags] production
[Setup] Test Setup #this is your custom KW, which will overwrite Test Setup
log to console Test1
${success} = Run Keyword and Return Status Submit The Job #Code will not switch to next keyword untill this keyword return the satus as True or False
log ${success}
wait until keyword succeeds 2x 200ms Submit The Job #This Kw will run the KW 2 times if KW fails while waiting 200ms after each result
*** Keywords ***
Submit The Job
Sleep 10s
Test Setup
log to console child setup
Now coming to your last point , you can use -e ie exclude option in tags to run specific test case现在到了最后一点,您可以在标签中使用-e ie exclude 选项来运行特定的测试用例
so the command will be所以命令将是
robot -i "Debug" -e "production" sample_tc.robot
This will execute only first TC, ie TC1这将只执行第一个 TC,即 TC1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.