如何在行为 .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 |
Run Code Online (Sandbox Code Playgroud)
小智 11
您可以将数据提供为 json,并json.loads在步骤中使用它进行解析。
请注意,要使用Examples:我们需要 aScenario Outline而不是 a
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"]|
Run Code Online (Sandbox Code Playgroud)
json.loads在步骤中使用解析它:
# 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"]|
Run Code Online (Sandbox Code Playgroud)
除了使用,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
Run Code Online (Sandbox Code Playgroud)
# 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4006 次 |
| 最近记录: |