【问题标题】:How to expect a failure in robot framework in Python?如何预期 Python 中的机器人框架会失败?
【发布时间】:2018-11-11 20:11:11
【问题描述】:

我们在工作中使用机器人框架做一些自动化测试,我需要添加一些测试。这种格式已经在 repo 中了,我不能彻底改变任何东西。

我正在使用如下所示的关键字文件:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from robot.api.deco import keyword

class testkeywords(object):
   """
   """

   def __init__(self):
      pass

   @keyword('I compare ${first_number} with ${second_number}')
   def compare(self, first_number, second_number):
      if (int(first_number) != int(second_number)):
         raise ValueError('Numbers are not the same.')

.robot 文件有两个测试,一个通过,一个失败:

*** Settings ***
Library         testkeywords.py

*** Variables ***
${num_a}       5
${num_b}       6

*** Test Cases ***
Compare same numbers
   I compare ${num_a} with ${num_a}

Compare different numbers
   I compare ${num_a} with ${num_b}

Compare different numbers 测试按预期失败,但仍然失败。如何将其设置为预期失败并因此传递关键字?

【问题讨论】:

  • 反对者请解释。我提供了一个最小的例子,问题很清楚。
  • 在我看来,这个问题写得很好,提供的例子也很清楚。是什么让我想到,也许也是反对者,是这个问题很快就得到了你自己的回答。这向我表明,花在研究上的时间并不多。鉴于解决方案是一个开箱即用的关键字,这对我来说也是如此。从经验中学习,也许在提问之前阅读有关调查级别的帮助。
  • @A.Kootstra,虽然我确实在搜索中看到了关键字,但我没有注意到要识别关键字所需的双倍行距。我正在查看的 .robot 文件的格式与我在网上看到的任何内容都不匹配,我认为它可能需要不同的关键字。我实际上花了几个小时玩它,直到我注意到双倍空间。也许框架的文档应该更好一些。对于它的价值,大多数关于 SO 的 python 问题都可以通过查看文档来解决。至少我费心发布答案......

标签: python robotframework


【解决方案1】:

您还可以使用以下格式从完整错误中获得子字符串:

Compare different numbers
   Run Keyword And Expect Error  *are not the same*  I compare ${num_a} with ${num_b}

文档:Run Keyword And Expect Error

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
【解决方案2】:

我想通了。您可以只修改测试以预期出现如下错误:

Compare different numbers
   Run Keyword And Expect Error  *  I compare ${num_a} with ${num_b}

或者查找特定错误(而不是通配符):

Compare different numbers
       Run Keyword And Expect Error  ValueError: Numbers are not the same.  I compare ${num_a} with ${num_b}

注意命令、错误和关键字之间的双空格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2018-11-18
    • 2021-07-17
    • 2021-04-22
    • 1970-01-01
    • 2016-08-05
    • 2019-04-19
    相关资源
    最近更新 更多