【问题标题】:pass in multiple arguments in lettuce test step在生菜测试步骤中传入多个参数
【发布时间】:2012-08-27 08:36:19
【问题描述】:

通常每个生菜测试步骤都有一个参数,有没有办法在一个步骤中传递多个参数?

喜欢,我可以要这个吗:

@step('I have the number (\d+) and character (\w+)')
def have_the_number(step, number, character ):
    world.number = int(number)
    world.character = str(character)

【问题讨论】:

    标签: python testing lettuce


    【解决方案1】:

    您的代码完全有效。您可以同时使用位置参数(如 *args,就像在您的示例中一样)以及命名参数(如 **kwargs)。

    假设您有以下math.feature

    Feature: Basic computations
        In order to play with Lettuce
        As beginners
        We will implement addition and subtraction
    
        Scenario: Sum of 0 and 1
            Given I have to add the numbers 0 and 1
            When I compute its factorial
            Then I see the number 1
    
        Scenario: Difference of 3 and 5
            Given I have to substract 5 from 3
            When I compute their difference
            Then I see the number -2
    

    等等steps.py:

    from lettuce import *
    
    @step('I have to add the numbers (\d+) and (\d+)')
    def have_to_add(step, number1, number2):
        world.number1 = int(number1)
        world.number2 = int(number2)
    
    @step('I have to substract (?P<subtrahend>) from (?P<minuend>)')
    def have_to_substract(step, minuend, subtrahend):
        world.minuend = int(minuend)
        world.subtrahend = int(subtrahend)
    
    @step('I compute their difference')
    def compute_difference(step):
        world.number = world.minuend - world.subtrahend
    
    @step('I compute their sum')
    def compute_sum(step):
        world.number = world.number1 + world.number2
    
    @step('I see the number (\d+)')
    def check_number(step, expected):
        expected = int(expected)
        assert world.number == expected, "Got %d" % world.number
    

    仔细看看减法示例,它展示了如何通过名称而不是位置来引用捕获的变量。

    【讨论】:

      【解决方案2】:

      是什么阻止你这样做? 您可以在一个步骤中使用多个参数,就像您的示例所示。

      我猜步骤名称只是被解析为正则表达式模式,匹配的组将作为参数传递到步骤处理程序中。

      【讨论】:

      • 只是不确定,因为没有示例以这种方式使用它,是否有链接到此用法的文档项?反正我会验证的。
      • 我在我的项目中一直使用它。你面临什么问题让你相信它不会起作用?
      • 没问题,只是我的项目中没有人这样使用它,然后确定。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多