Skip to main content
 首页 » 编程设计

python之检查响应是否包含 Robot Framework 中的值列表

2025年05月04日15freeliver54

我是机器人的新手,如果这是一个愚蠢的问题,我深表歉意,但我正在寻找将列表传递给内置方法 should_contain 的方法:

def should_contain(self, item1, item2, msg=None, values=True): 
    """Fails if `item1` does not contain `item2` one or more times. 
 
    Works with strings, lists, and anything that supports Python's `in` 
    keyword. See `Should Be Equal` for an explanation on how to override 
    the default error message with `msg` and `values`. 
 
    Examples: 
    | Should Contain | ${output}    | PASS | 
    | Should Contain | ${some_list} | value  | 
    """ 
    msg = self._get_string_msg(item1, item2, msg, values, 'does not contain') 
    asserts.fail_unless(item2 in item1, msg) 

所以首先是测试用例中的简单方法,我知道这种语法是错误的,但是否可以做类似的事情:

Should Contain    ${RESPONSE}    "hello","world",250 

我尝试使用列表变量并将其传入,这似乎可行,但在更深入的调查中,它似乎只是比较元素计数而不是令人失望的实际元素值

为了解决这个问题,我刚刚对我关心的值做了 Should Contain。问题是我在多行上重复 Should Contain 和不同的数据,这显然不是好的做法。

Should Contain  ${RESPONSE}    "Hello" 
Should Contain  ${RESPONSE}    "World" 
Should Contain  ${RESPONSE}    250 

是否有人能够提供一些指导或改进方法来实现我正在尝试做的事情?我正在考虑创建一种新方法以允许它进入自定义库,但我认为必须有一种更简单的方法。需要明确的是,我不关心项目的顺序。

请您参考如下方法:

Should Contain 关键字不接受列表类型变量作为第二个参数。您可以通过使用 FOR 循环来解析列表来解决此问题:

*** Variables *** 
${Response}    "250 hello world foobar" 
 
*** Test Cases *** 
Stackoverflow 
@{list} =   Create List   hello   world    250 
:FOR    ${item}    in     @{list} 
\       Should Contain    ${RESPONSE}    ${item}