You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python代码中运行时跳过Robot Framework测试?

在Python代码中实现Robot Framework测试运行时跳过

无需依赖Robot Framework自带的跳过关键字,直接在Python测试库中抛出SkipExecution异常即可实现运行时跳过测试,适用于计算器组件不可用等场景。以下是完整实现方案:

原Robot Framework测试用例

*** Keywords ***
Calculate
    [Arguments]    ${expression}    ${expected}
    Push buttons    C${expression}=
    Result should be    ${expected}

Calculation should fail
    [Arguments]    ${expression}    ${expected}
    ${error} =    Should cause error    C${expression}=
    Should be equal    ${expected}    ${error}    # 使用`BuiltIn`关键字

原Python测试库代码

class CalculatorLibrary(object):
    """测试库,用于测试*计算器*业务逻辑。
    通过计算器的``push``方法直接与其交互。
    """
    
    is_skip = False
    
    def result_should_be(self, expected):
        """验证当前结果是否为``expected``。
        示例:
        | Push Buttons     | 1 + 2 = |
        | Result Should Be | 3       |
        """
        if self._result != expected:
            is_skip = True
            # 需要标记测试为跳过
            raise AssertionError('%s != %s' % (self._result, expected))

    def should_cause_error(self, expression):
        """验证计算给定``expression``时会触发错误。
        返回的错误信息可通过`BuiltIn`库中的`Should Be Equal`等关键字验证。
        示例:
        | Should Cause Error | invalid            |                   |
        | ${error} =         | Should Cause Error | 1 / 0             |
        | Should Be Equal    | ${error}           | Division by zero. |
        """
        try:
            self.push_buttons(expression)
        except Exception as err:
            return str(err)
        else:
            raise AssertionError("'%s' should have caused an error." % expression)

修改后的Python实现(支持运行时跳过)

from robot.api import SkipExecution

class CalculatorLibrary(object):
    """测试库,用于测试*计算器*业务逻辑。
    通过计算器的``push``方法直接与其交互。
    """
    
    def __init__(self):
        # 全局检查:初始化时判断计算器是否可用,不可用则跳过所有测试
        if not self._check_calculator_status():
            raise SkipExecution("测试环境计算器组件不可用,跳过所有相关测试")

    def _check_calculator_status(self):
        """替换为实际的组件可用性检查逻辑
        比如ping服务、调用健康检查接口等
        """
        # 示例:模拟组件不可用的情况
        return False

    def result_should_be(self, expected):
        """验证当前结果是否为``expected``。
        示例:
        | Push Buttons     | 1 + 2 = |
        | Result Should Be | 3       |
        """
        # 单步骤检查:也可在单个关键字中触发跳过
        if not self._check_calculator_status():
            raise SkipExecution("计算器组件不可用,跳过当前测试步骤")
            
        if self._result != expected:
            raise AssertionError(f"{self._result} != {expected}")

    def should_cause_error(self, expression):
        """验证计算给定``expression``时会触发错误。
        返回的错误信息可通过`BuiltIn`库中的`Should Be Equal`等关键字验证。
        示例:
        | Should Cause Error | invalid            |                   |
        | ${error} =         | Should Cause Error | 1 / 0             |
        | Should Be Equal    | ${error}           | Division by zero. |
        """
        if not self._check_calculator_status():
            raise SkipExecution("计算器组件不可用,跳过当前测试步骤")
            
        try:
            self.push_buttons(expression)
        except Exception as err:
            return str(err)
        else:
            raise AssertionError(f"'{expression}' should have caused an error.")

核心要点

  • 导入robot.api.SkipExecution异常,Robot Framework会自动识别该异常并将测试标记为跳过状态
  • 两种跳过方式:
    1. 全局跳过:在库的__init__方法中检查组件状态,不可用则直接抛出异常,跳过所有使用该库的测试
    2. 单步骤跳过:在单个关键字方法中检查,仅跳过当前测试步骤或用例
  • _check_calculator_status方法需根据实际测试环境替换为真实的可用性验证逻辑

内容的提问来源于stack exchange,提问作者JavaMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 05:05:16