首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在lettuce测试步骤中传入多个参数

在lettuce测试步骤中传入多个参数
EN

Stack Overflow用户
提问于 2012-08-27 16:36:20
回答 2查看 1.7K关注 0票数 3

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

就像,我能要这个吗:

代码语言:javascript
复制
@step('I have the number (\d+) and character (\w+)')
def have_the_number(step, number, character ):
    world.number = int(number)
    world.character = str(character)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-27 17:03:27

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

假设您有以下math.feature

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-08-27 16:51:46

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12138650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档