Pytest Bdd从给定条件下的小黄瓜中获取表值

2024-03-29 10:07:26 发布

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

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):
    ??

我怎么才能得到数据集、startdate、enddate表的值?我很困惑


Tags: thetabletooldetailsamdatasetaregiven
1条回答
网友
1楼 · 发布于 2024-03-29 10:07:26

一种方法是将表数据作为json加载到功能文件中。 将表数据存储在namedtuple中以便于访问。 将给定步骤定义为目标_fixture,以便其他步骤可以声明和使用表数据。 使用Python3.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 ===========================================================================



相关问题 更多 >