繁体   English   中英

如何在机器人框架的套件设置中“运行关键字 if tag="debug"”

[英]How to "run a keyword if tag="debug"" in suite setup in robot framework

我是机器人框架的新手。 我有一个 .robot 文件,其中有 5 个测试用例,我为每个测试用例定义了标签。 即 3 个测试用例有[tags] debug ,两个测试用例有[tags] preprod

现在我有一个套件设置和套件拆卸,其中对于那些具有标签调试的测试用例将执行某些步骤,而对于标签预生产不需要执行相同的步骤

例如:

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

现在 TC4 和 TC5 不需要在套件设置中执行Create An Employee和在套件拆卸中Delete DB entries

如何实现如果测试用例有tag=Debug执行套件设置和套件拆卸中的步骤

套件设置在任何测试开始之前运行一次。 不可能让它为每个测试做不同的事情。

如果要为每个测试执行某些操作,则需要使用测试设置 例如,如果测试具有“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

继续布莱恩的回答。 您需要了解设置部分中定义的测试设置将适用于所有测试用例。

因此,对于Debugpreprod标签,您将执行相同的测试设置。

简而言之,您需要基于标签设置不同的测试。

为了实现这一点,您需要在您打算使用特定标签的测试用例中使用同名的自定义关键字,即测试设置

这是一个执行相同操作的示例。

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

现在到了最后一点,您可以在标签中使用-e ie exclude 选项来运行特定的测试用例

所以命令将是

robot -i "Debug" -e "production" sample_tc.robot

这将只执行第一个 TC,即 TC1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM