0

我想在没有桌面项目的 Gherkin 测试中调用场景 - 比如说 500 次。原因是我想使用随机变量而不是自己编写。我知道如何在测试中实现随机功能,但它只被调用一次。

例如 :

Scenario Outline: I want to test speed with different values
    When I set the speed to <speed>
    And I wait for 5 seconds
    Then it plays at <speed>

    Examples:
    | speed       |
    | 10   |
    | 20   |
    | 30   |
    | 40   |
    | 50   |

随机导入

速度 = ['10', '20', '30', '40', '50']

def next_speed(): return random.choice(speeds)

如果我使用这样的随机功能,我怎么能调用这个场景 500 次?

提前致谢。

4

5 回答 5

0
from __future__ import print_function
import functools
from behave.model import ScenarioOutline


def patch_scenario_with_autoretry(scenario, max_attempts=3):
    """Monkey-patches :func:`~behave.model.Scenario.run()` to auto-retry a
    scenario that fails. The scenario is retried a number of times
    before its failure is accepted.
    This is helpful when the test infrastructure (server/network environment)
    is unreliable (which should be a rare case).
    :param scenario:        Scenario or ScenarioOutline to patch.
    :param max_attempts:    How many times the scenario can be run.
    """
    def scenario_run_with_retries(scenario_run, *args, **kwargs):
        for attempt in range(1, max_attempts+1):
            if not scenario_run(*args, **kwargs):
                if attempt > 1:
                    message = u"AUTO-RETRY SCENARIO PASSED (after {0} attempts)"
                    print(message.format(attempt))
                return False    # -- NOT-FAILED = PASSED
            # -- SCENARIO FAILED:
            if attempt < max_attempts:
                print(u"AUTO-RETRY SCENARIO (attempt {0})".format(attempt))
        message = u"AUTO-RETRY SCENARIO FAILED (after {0} attempts)"
        print(message.format(max_attempts))
        return True

    if isinstance(scenario, ScenarioOutline):
        scenario_outline = scenario
        for scenario in scenario_outline.scenarios:
            scenario_run = scenario.run
            scenario.run = functools.partial(scenario_run_with_retries, scenario_run)
    else:
        scenario_run = scenario.run
        scenario.run = functools.partial(scenario_run_with_retries, scenario_run)

参考:https ://github.com/behave/behave/blob/master/behave/contrib/scenario_autoretry.py

于 2020-08-19T03:32:51.587 回答
0

在过去的几年里,我一直在考虑这个问题,而我一直在使用 Behave。我用它来驱动关键通信无线电的功能测试环境,以及它们所处的环境(播放/录制音频,对 WAV 文件进行一些深度学习以确认内容,使用它们的 UI 并四处导航以创建消息, 像这样的东西)。

我以前需要尝试在提供数据表的单行之外尝试循环。

当我在调查 Content 对象的一些凹处时,我深入研究了解释器,我想知道我是否可以设法让它实现这样的东西:

    When I loop i 5 times
      And I perform this action
      And I perform another action
    Then I should see this result
      And loop back to i

这不会破坏 gherkin 语法,并且如果我可以在循环步骤中实现一些东西,它会以某种方式倒回解析器,它应该再次运行它们。我怀疑,困难的部分是确保所有步骤的结果都得到保留:我需要深入研究用于存储结果的结构,以便输出显示所有迭代。

有没有其他人考虑通过定义的步骤将其实现到 Behave 中?

于 2019-11-05T12:19:07.940 回答
-1

如果您尝试将 gherkin 用作脚本工具,那么您将度过一段糟糕的时光。有更好的工具,比如 python 本身或机器人框架。问问自己,您期望从小黄瓜测试中获得什么优势。你的小黄瓜应该回答“为什么”你在做某事,它应该有充分解释不同情况的例子——最好只有有趣的例子。

于 2016-07-06T15:31:46.680 回答
-1

您需要每次添加行动态进行测试。它永远不会显示在功能文件中,但需要添加行。

以下链接几乎没有可以在步骤定义中为您创建动态行的功能

http://www.programcreek.com/java-api-examples/index.php?api=gherkin.formatter.model.DataTableRow

或从步骤定义中调用步骤定义

http://www.specflow.org/documentation/Calling-Steps-from-Step-Definitions/

于 2016-07-07T14:57:09.560 回答
-1

希望我能理解你的问题:)

那这个呢:

将步骤:“当我将速度设置为速度时”更改为

当我将速度设置为 {speed} 时,它需要一个参数。

在您的功能中:当我测试速度 500 次并且在那一步中:当我测试速度 500 次时:

==> 创建一个 for 循环 500 次:

=====>选择随机速度

=====>使用 context.execute_steps 和 format(speed) 执行其他步骤

你必须解决这个问题,因为它需要 unicode,而不是整数。

==> 但是,有人可能同意 Szabo Peter 的观点,即为此使用 gherkin/python-behave 有点尴尬:)。这有点搞砸了目的。此外,即使按照我的想法,它可能会做得更优雅。

你可以在这里找到一些好东西:https: //jenisys.github.io/behave.example/tutorials/tutorial08.html Cheerz

所以在评论后编辑:(在编辑和编写这个例子之后,它看起来比我想象的更傻,所以是的:不要为此使用行为。例如:

功能:测试功能场景:给定的测试场景当我以随机速度测试应用程序 500 次时打开应用程序,然后控制台说它完成了

脚步:

@given(u'我打开应用')

=>def I_open_the_app(上下文):

==>#code 打开应用程序

@when(u'我以随机速度测试应用程序 500 次')

=>def I_test_the_app_500_times_at_random_speed(上下文):

==>对于范围内的时间(1,500):

===>random_speed = random.randint(min_speed,max_speed)

===>context.execute_steps(u'''when I play at {speed}'''.format(speed=str(random_speed))

@when(u'我用 {speed}')

=>def I_play_at(上下文,速度)

==>play_at_speed(int(速度))

@then(u'控制台说它完成了')

=>def the_console_says_it_is_done ==>print('完成')

于 2016-07-28T18:28:22.287 回答