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



概述我有一个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,670,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,670,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,670,671  0222  123456789n622,670,671 0444 123456789

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

最新更新