通常每个生菜测试步骤都需要一个参数,有没有办法在一个步骤中传递多个参数?
像,我可以有这个:
@step('I have the number (\d+) and character (\w+)')
def have_the_number(step, number, character ):
world.number = int(number)
world.character = str(character)
您的代码完全有效。您可以同时使用位置参数(如*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
仔细看看减法示例,它显示了如何通过名称而不是位置来引用捕获的变量。
是什么阻止你这样做?就像您的示例所示,您可以在一个步骤中使用多个参数。
我猜步骤名称只是被解析为正则表达式模式,匹配的组将作为参数在步骤处理程序中传递。