如何在python中传递诸如列表或字典之类的对象的行为.feature文件

时间:2018-10-03 20:16:15

标签: python-behave

如何在行为.feature文件中传递诸如列表或字典之类的对象作为参数,以便在python函数步骤中使用该参数?在下面查看我要实现的示例:

Feature:
Scenario: Given the inputs below
    Given a "<Dictionary>" and  "<List>"
    When we insert "<Dictionary>" and  "<List>"
    Then we confirm the result in the database

    Examples: Input Variables
        |Input1                    |Input2    |
        |Dictionary(json)          |List      |

1 个答案:

答案 0 :(得分:2)

您可以将数据提供为json,并在步骤中使用Examples:进行解析。

请注意,要使用Scenario Outline,我们需要Scenario而不是 # features/testing_objects.feature Feature: Testing objects Scenario Outline: Given the inputs below Given a <Dictionary> and <List> When we insert them Then we confirm the result in the database Examples: Input Variables |Dictionary |List | |{"name": "Fred", "age":2} |[1,2,"three"]|

json.loads

在步骤中使用# features/steps/steps.py import json from behave import given, when, then @given('a {dictionary} and {a_list}') def given_dict_and_list(context, dictionary, a_list): context.dictionary = json.loads(dictionary) context.a_list = json.loads(a_list) @when('we insert them') def insert_data(context): print('inserting dictionary', context.dictionary) print('inserting list', context.a_list) @then('we confirm the result in the database') def confirm(context): print('checking dictionary', context.dictionary) print('checking list', context.a_list) 对其进行解析:

Examples:

除了使用context.text之外,您还可以使用多行字符串文字 然后通过Feature: String literal JSON Scenario: Given a dictionary """ { "name": "Fred", "age": 2 } """ And a list """ [1, 2, "three"] """ Then we can check the dictionary And check the list 在单独的步骤中访问每个对象。

@given('a dictionary')
def given_a_dictionary(context):
    context.dictionary = json.loads(context.text)

@given('a list')
def given_a_list(context):
    context.a_list = json.loads(context.text)

@then('we can check the dictionary')
def check_the_dictionary(context):
    assert context.dictionary == {
        'name': 'Fred',
        'age': 2
    }

@then('check the list')
def check_the_list(context):
    assert context.a_list == [1, 2, 'three']
ncUseData <- select(rollData, session, UID, name)

ncUseData <- ncUseData %>% group_by(session, UID) %>% mutate(countUp = n())