首页 > 解决方案 > 如何预期 Python 中的机器人框架会失败?

问题描述

我们在工作中使用机器人框架做一些自动化测试,我需要添加一些测试。这种格式已经在 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按预期失败,但仍然失败。如何将其设置为预期失败并因此传递关键字?

标签: pythonrobotframework

解决方案


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

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}

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


推荐阅读