通常每个生菜测试步骤采用1个参数,是否可以在一个步骤中传递多个参数?
喜欢,我可以有这个:
@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
以及诸如以下步骤:
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
仔细看一下减法示例,它显示了如何通过名称而不是位置来引用捕获的变量.