Robot Framework 中相互依赖的测试自动失败/不执行

问题描述

我有大量的测试用例,其中几个测试用例是相互依赖的.是否有可能在执行稍后的测试用例时,您可以找出先前执行的测试用例的状态?就我而言,第 99 个测试用例取决于一些先前测试用例的状态,因此,如果第 24 个或第 38 个测试用例失败,我希望第 99 个测试用例根本不执行,从而节省大量时间.如果可能的话,请用一些例子来解释.提前致谢!

I have a large number of test cases, in which several test cases are interdependent. Is it possible that while a later test case is getting executed you can find out the status of a previously executed test case? In my case, the 99th test case depends on the status of some prior test cases and thus, if either the 24th or the 38th fails I would like the 99th test case NOT to get executed at all and thus save me a lot of time. Kindly, explain with some example if possible. Thanks in advance!


解决方案

一旦机器人开始运行,就无法根据某些条件跳过测试.我认为这是机器人的弱点之一,但设计者似乎真的不喜欢跳过测试的概念.此外,没有一种内置方式可以让一个测试依赖于另一个测试.对此功能的功能请求被拒绝.

Once robot starts running, there's no way to skip a test based on some condition. I think this is one of the weaknesses of robot, but the designers really don't seem to like the notion of skipped tests. Also, there's no built-in way for one test to depend on another. A feature request for this very feature was declined.

然而,robot 是非常可扩展的,并且在 2.8.5 版本中引入的一个功能可以很容易地编写一个关键字,如果另一个测试失败了就会失败.这个功能是 充当监听器的库.有了这个,图书馆可以跟踪每个测试的通过/失败状态.有了这些知识,您可以创建一个在其他测试失败时立即失败的关键字.

However, robot is very extensible, and a feature that was introduced in version 2.8.5 makes it easy to write a keyword that will fail if another test has failed.This feature is the ability for a library to act as a listener. With this, a library can keep track of the pass/fail status of each test. With that knowledge, you can create a keyword that fails immediately if some other test fails.

基本思想是,在每次测试完成时缓存通过/失败状态(通过特殊的 _end_test 方法).然后,使用这个值来判断是否立即失败.

The basic idea is, cache the pass/fail status as each test finishes (via the special _end_test method). Then, use this value to determine whether to fail immediately or not.

以下是如何使用此类关键字的示例:

Here's an example of how to use such a keyword:

*** Settings ***
| Library | /path/to/DependencyLibrary.py

*** Test Cases ***
| Example of a failing test
| | fail | this test has failed

| Example of a dependent test
| | [Setup] | Require test case | Example of a failing test
| | log | hello, world

这是库定义:

from robot.libraries.BuiltIn import BuiltIn

class DependencyLibrary(object):
    ROBOT_LISTENER_API_VERSION = 2
    ROBOT_LIBRARY_SCOPE = "GLOBAL"

    def __init__(self):
        self.ROBOT_LIBRARY_LISTENER = self
        self.test_status = {}

    def require_test_case(self, name):
        key = name.lower()
        if (key not in self.test_status):
            BuiltIn().fail("required test case can't be found: '%s'" % name)

        if (self.test_status[key] != "PASS"):
            BuiltIn().fail("required test case failed: '%s'" % name)

        return True

    def _end_test(self, name, attrs):
        self.test_status[name.lower()] = attrs["status"]

相关文章