0
Scenario Outline: scenario sample
Given the following datasource details are given
  | dataSet      |  startDate   | endDate   |
  | <dataSource> | <startDate> | <endDate> |
When the user wants to "<tool>"
Then usage count is "<expectedUsageCount>"
 Examples:
  |dataSet | startDate           |endDate              |tool               | expectedUsageCount|                 
  |dataSet1| 11/9/2018, 10:31 AM |  11/9/2020, 10:31 AM| ToolName_8        |              123 |  
  |dataSet2|  11/9/2020, 10:31 AM|11/9/2022, 10:31 AM  |ToolName_17         | 345             |  

我曾尝试将 Pytest bdd 用于上述 senario 大纲。我想问的是如何用于给定的表?

??

> @given(parsers.parse('the following datasource details are given\n{attr_table}'))  
def selectDataSource(datatable,attr_table):
    ??

如何获取我感到困惑的数据集、开始日期、结束日期表值?

4

1 回答 1

0

一种方法是将特征文件中的表数据加载为 json。将表数据存储在命名元组中以便于访问。将给定步骤定义为 target_fixture,以便其他步骤可以声明和使用表数据。使用 Python 3.7.5、pytest-6.2.4、'bdd': '4.0.2' 测试

  @loadtable
  Scenario: Load Table Data
    Given Load table data as json
      [
        ["Name",   "Type",  "IP_Address",  "Mask",    "Distance"],
        ["Name1",  "http",  "171.21.1.11", "171/24",  "101"   ],
        ["Name2",  "https", "172.20.2.22", "173/24",  "102"   ]
      ]
    Then Use the table data


@given(parsers.parse('Load table data as json\n{table_data:json}', extra_types=dict(json=json.loads)), target_fixture="mytabledata")
def step_load_table_data(table_data):
    MyTableData = namedtuple("MyTableData", table_data[0])
    data = [MyTableData._make(d) for d in table_data]
    return data


@then('Use the table data')
def step_use_table_data(mytabledata):
    print(f"mytabledata {pformat(mytabledata)}")


❯ pytest -v -s -m loadtable
>>>>> no number of cores provided or running in environment unsupportive of parallelized testing, not running multiprocessing <<<<<
================================================================================= test session starts ==================================================================================
platform darwin -- Python 3.7.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/kwong/venv/pytests/bin/python
cachedir: .pytest_cache
benchmark: 3.4.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
metadata: {'Python': '3.7.5', 'Platform': 'Darwin-19.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'benchmark': '3.4.1', 'cov': '2.12.0', 'bdd': '4.0.2', 'flaky': '3.7.0', 'html': '3.1.1', 'mproc': '4.1.0', 'xdist': '2.2.1', 'metadata': '1.11.0', 'forked': '1.3.0', 'testmon': '1.1.1'}}
rootdir: /Users/kwong/testing/saas/kennethsaas2/tests, configfile: pytest.ini
plugins: benchmark-3.4.1, cov-2.12.0, bdd-4.0.2, flaky-3.7.0, html-3.1.1, mproc-4.1.0, xdist-2.2.1, metadata-1.11.0, forked-1.3.0, testmon-1.1.1
collected 12 items / 11 deselected / 1 selected

step_defs/v2api/test_v2api.py::test_load_table_data mytabledata [MyTableData(Name='Name', Type='Type', IP_Address='IP_Address', Mask='Mask', Distance='Distance'),
 MyTableData(Name='Name1', Type='http', IP_Address='171.21.1.11', Mask='171/24', Distance='101'),
 MyTableData(Name='Name2', Type='https', IP_Address='172.20.2.22', Mask='173/24', Distance='102')]
PASSED

=========================================================================== 1 passed, 11 deselected in 0.08s ===========================================================================



于 2021-06-05T19:03:37.763 回答