首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在机器人框架中的套件设置中“运行关键字如果tag=”“调试”

如何在机器人框架中的套件设置中“运行关键字如果tag=”“调试”
EN

Stack Overflow用户
提问于 2020-03-17 06:04:08
回答 2查看 2.1K关注 0票数 0

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

现在,我有了一个套件设置和套件拆卸,对于那些具有标记的测试用例,调试将执行某些步骤,而对于标记preprod,不需要执行相同的步骤。

代表:

代码语言:javascript
运行
复制
*** 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,则在套件设置和套件拆卸中执行步骤

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-17 15:52:16

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

如果要对每个测试执行某些操作,则需要使用测试设置。例如,如果测试有"Debug“或"preprod”标记,则这里有一个关键字将记录不同的字符串:

代码语言:javascript
运行
复制
*** 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
票数 0
EN

Stack Overflow用户

发布于 2020-03-18 06:38:25

继续布赖恩的回答。您需要理解,在“设置”部分中定义的测试设置将适用于所有测试用例。

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

简而言之,您希望基于标记设置不同的测试。

要实现这一点,您需要使用同名的自定义关键字,即在您希望具有特定标记的测试用例中使用测试设置

下面是一个同样的例子。

代码语言:javascript
运行
复制
*** 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,即在标记中排除选项来运行特定的测试用例

所以命令是

代码语言:javascript
运行
复制
robot -i "Debug" -e "production" sample_tc.robot

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60716982

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档