【3】基数:一个或多个(类型列表)实例

基数:一个或多个(类型列表)

有时需要一个解决方案来解析一个或多个项的列表。最初,这应该是一个逗号分隔的列表,例如: 

Scenario:
    When I meet Alice
     And I meet Alice, Bob, Charly

然后,应该处理一个由“and”分隔的列表,例如:
Scenario:
    When I meet Alice and Bob and Charly

1.Feature Example

Feature:具有一个或多个基数的数据类型(MANY,List<T>)
  Scenario: 多列表,逗号分隔
    Given I go to a meeting
    When I meet Alice, Bob, Dodo
    And  I meet Charly
    Then the following persons are present:
      | name   |
      | Alice  |
      | Bob    |
      | Charly |
      | Dodo   |

  Scenario: 带有列表分隔符“和”的多个列表
    Given I go to a meeting
    When I meet Alice and Bob and Charly
    Then the following persons are present:
      | name   |
      | Alice  |
      | Bob    |
      | Charly |

2.定义数据类型

# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder


company_persons = ["Alice", "Bob", "Charly", "Dodo"]
parse_person = TypeBuilder.make_choice(company_persons)
#多列表 逗号分隔
parse_persons2 = TypeBuilder.with_many(parse_person)
type_dict = {"Person+": parse_persons2}
register_type(**type_dict)

# -- 多类型:人员:=列表<Person>,列表分隔符为“and”
# parse_persons = TypeBuilder.with_one_or_more(parse_person, listsep="and")
parse_persons = TypeBuilder.with_many(parse_person, listsep="and")
register_type(PersonAndMore=parse_persons)'''

注意
TypeBuilder.with_many()函数执行魔术。它为项目列表计算正则表达式模式。
然后,它生成一个类型转换器函数,该函数通过为一个项目(“Person”)使用类型转换器来处理项目列表。

3.提供步骤定义

# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then

# -- 多变量1:在解析表达式中使用基数字段(逗号分隔)
@when('I meet {persons:Person+}')
def step_when_I_meet_persons(context, persons):
    for person in persons:
        context.meeting.persons.add(person)

# -- 多变量2:使用特殊的多数据类型(“and”-分隔)
@when('I meet {persons:PersonAndMore}')
def step_when_I_meet_person_and_more(context, persons):
    for person in persons:
        context.meeting.persons.add(person)

# ----------------------------------------------------------------------------
# MORE STEPS:
# ----------------------------------------------------------------------------
from hamcrest import assert_that, contains

@given('I go to a meeting')
def step_given_I_go_to_meeting(context):
    context.meeting = Meeting()

@then('the following persons are present')
def step_following_persons_are_present(context):
    assert context.table, "table<Person> is required"
    actual_persons   = sorted(context.meeting.persons)
    expected_persons = [ row["name"]  for row in context.table ]

    # -- 列表断言:
    assert_that(actual_persons, contains(*expected_persons))

4.进行测试

F:\AAM_project\exercise_v1\adf_behaveTest>behave
Feature: Data Type with Cardinality one or more (MANY, List<T>) # features/step_2.feature:1

  Scenario: Many list, comma-separated     # features/step_2.feature:3
    Given I go to a meeting                # features/steps/step_2.py:62
    When I meet Alice, Bob, Dodo           # features/steps/step_2.py:46
    And I meet Charly                      # features/steps/step_2.py:46
    Then the following persons are present # features/steps/step_2.py:66
      | name   |
      | Alice  |
      | Bob    |
      | Charly |
      | Dodo   |

  Scenario: Many list with list-separator "and"  # features/step_2.feature:14
    Given I go to a meeting                      # features/steps/step_2.py:62
    When I meet Alice and Bob and Charly         # features/steps/step_2.py:52
    Then the following persons are present       # features/steps/step_2.py:66
      | name   |
      | Alice  |
      | Bob    |
      | Charly |

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
7 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.002s

F:\AAM_project\exercise_v1\adf_behaveTest>

 5.完整的代码:

#encoding:utf-8
#@Time:2020/4/11 20:59
#@Author:sunny

# ----------------------------------------------------------------------------
# DOMAIN MODEL:
# ----------------------------------------------------------------------------
class Meeting(object):
    def __init__(self):
        self.persons = set()


# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder


company_persons = ["Alice", "Bob", "Charly", "Dodo"]
parse_person = TypeBuilder.make_choice(company_persons)
#多列表 逗号分隔
parse_persons2 = TypeBuilder.with_many(parse_person)
type_dict = {"Person+": parse_persons2}
register_type(**type_dict)

# -- 多类型:人员:=列表<Person>,列表分隔符为“and”
# parse_persons = TypeBuilder.with_one_or_more(parse_person, listsep="and")
parse_persons = TypeBuilder.with_many(parse_person, listsep="and")
register_type(PersonAndMore=parse_persons)


'''
注意
TypeBuilder.with_many()函数执行魔术。它为项目列表计算正则表达式模式。
然后,它生成一个类型转换器函数,该函数通过为一个项目(“Person”)使用类型转换器来处理项目列表。
'''

# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then

# -- 多变量1:在解析表达式中使用基数字段(逗号分隔)
@when('I meet {persons:Person+}')
def step_when_I_meet_persons(context, persons):
    for person in persons:
        context.meeting.persons.add(person)

# -- 多变量2:使用特殊的多数据类型(“and”-分隔)
@when('I meet {persons:PersonAndMore}')
def step_when_I_meet_person_and_more(context, persons):
    for person in persons:
        context.meeting.persons.add(person)

# ----------------------------------------------------------------------------
# MORE STEPS:
# ----------------------------------------------------------------------------
from hamcrest import assert_that, contains

@given('I go to a meeting')
def step_given_I_go_to_meeting(context):
    context.meeting = Meeting()

@then('the following persons are present')
def step_following_persons_are_present(context):
    assert context.table, "table<Person> is required"
    actual_persons   = sorted(context.meeting.persons)
    expected_persons = [ row["name"]  for row in context.table ]

    # -- 列表断言:
    assert_that(actual_persons, contains(*expected_persons))

 

posted @ 2020-04-12 23:47  做一只热爱生活的小透明  阅读(299)  评论(0)    收藏  举报