Python行为可选参数

2024-04-20 13:31:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用Cucumber(Behave)编写一个测试。“给定”语句需要能够接受可选参数,它用于验证字符串中的内容

两种方案的功能语法示例如下:

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


Tags: totexttestclean参数stringisthis
2条回答

可选部分的正则表达式:(?: exclude "(?P<excluded>[^"]*?)")?

如果没有对所有步骤使用Regex解析器,请将步骤匹配器恢复为默认的'parse'

use_step_matcher('re')


@given('Verify text "(?P<text_to_clean>[^"]*?)"(?: exclude "(?P<excluded>[^"]*?)")?')
def step_verify_text(context, text_to_clean, excluded):
    context.verified = verify_string(text_to_clean, excluded)


use_step_matcher('parse')

注意:feature文件名和steps文件夹中的文件名应该匹配

例如:如果feature文件名是question_regex.feature,那么steps文件夹中的文件名应该是question_regex.py

一,。选项-Regex解析器

功能/问题\正则表达式功能

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"

功能/步骤/问题\u 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

二,。选项-CFParse

功能/步骤/问题\u 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"

功能/步骤/问题\u 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如果有多个可选的parameters相继出现,那么解析器不是一个好的选择

相关问题 更多 >