我正在尝试实施BDD步骤,如果该步骤是指单数或复数ex,则可以使用该步骤: 然后我应该看到显示名称“ John” 但是如果我有多个名字,我也想使用相同的步骤 然后我应该看到名称“ John,George”
在Java中,您可以在执行以下步骤时执行此操作: @Step(“那我应该看到名称了吗?(正则表达式)(:?是|被显示)”)
?-用于复数 and(:? |)是您要替换单词的
在功能文件中,键入(名称或名称;是或是)时,它指向同一步骤
有没有办法做到这一点?
答案 0 :(得分:0)
有关在BDD步骤中使用正则表达式的信息,请参见Using Step Patterns with Regular Expressions中的froglogic Squish manual。
基于此,以下对我有用:
# Use (?:...) because it is non-capturing
# Also see https://docs.python.org/2/library/re.html
@Then("I should see the (?:name|names) \"(.*)\" (?:is|are) displayed", regexp=True)
def step(context, nameOrNamesCommaSeparated):
"""Examples:
Then I should see the name "John" is displayed
Then I should see the names "John, George" are displayed
"""
names = []
if "," in nameOrNamesCommaSeparated:
names = nameOrNamesCommaSeparated.split(",")
for i, n in enumerate(names):
names[i] = n.strip()
else:
names = [nameOrNamesCommaSeparated]
for i, n in enumerate(names):
test.log("Name #%s: %s" % (i, n))