如何处理测试模板中的多行数据

问题描述

概述 我有一个 json,它提供多行和固定列

用例:在每次迭代中提取 0222 & 0444 和 0712 & 0786 的值以输入 json

*** Settings ***
Documentation  DataDriven Test
Test Template  Create CPR Data  
*** Variables ***


*** Test Cases ***   NPA0  CARRIER0  TEL0   
scenario1  684,670,671  0222  123456789
           ...  622,671  0444  123456789     # This data is multirow and this is the requirement 

scenario2  633,621,652  0712  123456789
       ...  626,470,671  0786  123456789


*** Keywords ***
Create CPR Data
    [Arguments]  ${npa_row1}  ${carrier_row1}  ${tel_row1}
    ${ReqBody_PR}  set variable  {"destNums":[{"NPA":"${carrier_row1}","CARRIER":"${carrier_row2}"}]}
    log  {ReqBody_PR}

PS:我不确定是否可以通过测试模板,只是想得到一些建议。

解决方法

有多种解决方案,但最直接的是根据数据对关键字进行建模。
您说所有场景都将有 2 行,每行有 3 个元素(“列”);在这种情况下,只需让关键字采用 6 个参数:

*** Test Cases ***   NPA1  CARRIER1  TEL1   NPA2  CARRIER2  TEL3
scenario1  684,670,671  0222  123456789
           ...  622,671  0444  123456789     # This data is multirow and this is the requirement 

scenario2  633,621,652  0712  123456789
       ...  626,470,671  0786  123456789


*** Keywords ***
Create CPR Data
    [Arguments]  ${npa_row1}  ${carrier_row1}  ${tel_row1}  ${npa_row2}  ${carrier_row2}  ${tel_row3}
    ${ReqBody_PR}  set variable  {"destNums":[{"NPA":"${carrier_row1}","CARRIER":"${carrier_row2}"}]}   # in your original question,you were already referencing a variable carrier_row2 - but you haven't defined it already; now it works
    log  {ReqBody_PR}
    # you can also access now the 0222 and 0444 from scenario1 directly:
    Log    tel1: ${tel_row1} tel2: ${tel_row2}

现在,如果出于 - 原因 - 您希望在值中包含换行符,则可以使用 \n 字符;例如类似的东西

scenario1  684,671  0222  123456789\n622,671 0444 123456789

,它将被您的原始关键字接受。但是 - 这不是类似表格的数据(行和列) - 第三个参数将是一个字符串,您可以先在换行符上拆分它 - 以获得第一行的最后一个元素,然后 - 在空白字符上,获取第二行的 3 个元素。
这非常麻烦且容易出错;为了完整起见,我添加了 \n 解决方案,您的问题也包括在内。