作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Cucumber (Behave) 编写测试。 'given' 语句需要能够带一个可选参数,它用于验证字符串中的内容。
这两种情况的示例功能语法是:
Given Verify text "this is a test string"
When String validation is "success"
Then Store form contents.
Given Verify text "this is a test string" exclude "test,trial,hello"
When String validation is "success"
Then Store form contents.
我尝试实现的步骤是:
# Verify text "this is a test string" exclude "test,trial,hello"
@given('Verify text "{text_to_clean}" {?: exclude "{excluded}')
def step_verify_text(context, text_to_clean, excluded):
context.verified = verify_string(text_to_clean, excluded)
我该怎么做?我尝试使用 ?
的可选符号,但我无法弄明白。我需要做什么才能使用可选参数?
我在 macOS Catalina 上使用 Mac。
最佳答案
注意 feature
文件名和steps
文件夹中的文件名应该匹配。
例如:如果 feature
文件名是 question_regex.feature
那么 steps
文件夹中的文件名应该是 question_regex.py
features/question_regex.feature
Feature: regex question
Scenario: first question regex
Given Verify text "this is a string"
Then parameter "excluded" is ""
Scenario: second question regex
Given Verify text "this is a test string" exclude "test,trial,hello"
Then parameter "excluded" is "test,trial,hello"
features/steps/question_regex.py
from behave import use_step_matcher, given, when, then
use_step_matcher("re")
@given('Verify text "(?P<text_to_clean>.*?)"((?: exclude )"(?P<excluded>.*?)")?')
def step_verify_text(context, text_to_clean, excluded):
context.myvar = excluded
@then('parameter "(?P<name>.*?)" is "(?P<result>.*?)"')
def step_verify_text(context, name, result):
return getattr(context, name, None) == result
特征/步骤/question_cfparse.feature
Feature: cfparse question
Scenario: first question cfparse
Given Verify text 2 "this is a test string"
Then normal parameter "excluded" is ""
Scenario: second question cfparse
Given Verify text 2 "this is a test string" exclude "test,trial,hello"
Then normal parameter "excluded" is "test,trial,hello"
Scenario: second question cfparse
Given Verify text 3 "this is a test string" exclude "hi,hello,I"
Then normal parameter "excluded" is "hi,hello,I"
功能/步骤/question_cfparse.py
from behave import given, when, then, use_step_matcher, register_type
import parse
use_step_matcher("cfparse")
@parse.with_pattern(r'(?: exclude )"(.*?)"')
def parse_word_optional(text):
print(text)
return text.strip()
@parse.with_pattern(r'(?: exclude )')
def parse_exclude(text):
return text.strip()
@parse.with_pattern(r'"(.*?)"')
def parse_word_between_quote(text):
return text.strip()
register_type(my1_=parse_exclude)
register_type(quote_word=parse_word_between_quote)
register_type(my_=parse_word_optional)
@given('Verify text 2 "{text_to_clean}"{ignore1:my1_?}{excluded:quote_word?}')
def step_verify_text(context, text_to_clean, ignore1, excluded):
context.excluded = excluded
@given('Verify text 3 "{text_to_clean}"{excluded:my_?}')
def step_verify_text(context, text_to_clean, excluded):
context.excluded = excluded
@then('normal parameter "{name}" is {result:quote_word*}')
def step_verify_text(context, name, result):
return getattr(context, name, None) == result
注意 如果您有多个可选的参数
一个接一个地出现,cfparse
解析器不是一个好的选择
关于Python Behave 可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65430742/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!