我正在机器人框架上编写代码,它是关于比较excel文件和mysql数据库之间的数据。在我的代码中,我可以成功比较数据,但是也可以比较不匹配,我想知道在哪一列中。如果需要更详细地说明,我有66列,并且知道它们的名称,因为它是用excel文档编写的,让我们认为excel和数据库之间存在2个不匹配的地方,我编写的代码给了我错误的值,例如
1) Lists are different:
Index 0: 905390000510 != 905390000511
2) Lists are different:
Index 0: 88975322100222 != 88975322100332
但是每次将其写入索引0时,我都不知道错误在哪里。因此,我需要在任何地方添加if子句到我的代码中,但是我还没有实现它,因为当我想使用RUN KEYWORD
IF语句时,我得到了所有列名,但我只需要得到2。解决这个问题?我在下面添加我的代码。
*** Test Cases ***
open first excel file
connect to database pymysql testdb root rootpass localhost 3306
:FOR ${i} IN RANGE 2 67
\ open excel document ${path1} ${i}
\ ${cname} read excel cell 1 ${i} Sheet2
\ ${value1}= read excel cell 2 ${i} Sheet2
\ @{list1} create list ${value1}
\ @{queryResults}= query select a_${i} from test_table where a_1 = 'filename1'
\ run keyword and continue on failure lists should be equal ${list1} @{queryResults}
\ run keyword if "${list1}" !=" ${queryResults}" log to console ${cname}
我的excel文件在第一行中包含列名,在第二行中有数据。
答案 0 :(得分:2)
尽管您的脚本包含一个列表比较,但当我仔细看时,这似乎不是列表比较,而是一个值比较,它会导致显示类似消息
在“ 1”行的“ BBBB”列中的值为2222!= 4444。
下面是一个示例,其中比较两个列表的值,并在生成错误消息时从第三个列表中检索列名:
*** Settings ***
Library Collections
*** Test Cases ***
Compare List
${ListColumns} Create List AAAA BBBB CCCC
${ListA} Create List 1111 2222 3333
${ListB} Create List 1111 4444 3333
FOR ${i} IN RANGE 0 2
run keyword and continue on failure
... should be equal as Strings
... @{ListA}[${i}]
... @{ListB}[${i}]
... values=False
... msg=In Row "${1}" Column "${ListColumns}[${i}]" value @{ListA}[${i}] != @{ListB}[${i}].
END
此脚本将产生以下控制台输出:
==============================================================================
Compare List | FAIL |
In Row "1" Column "BBBB" value 2222 != 4444.
------------------------------------------------------------------------------
SO004.Compare | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
SO004 | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================
请牢记这一点,您可以通过这种方式将其应用于脚本。请记住,以下代码未经测试,但应该可以使用。
Open First Excel File
Connect To Database pymysql testdb root rootpass localhost 3306
FOR ${i} IN RANGE 2 67
# Get the value and column
open excel document ${path1} ${i}
${ColumnName} read excel cell 1 ${i} Sheet2
${Excelvalue} read excel cell 2 ${i} Sheet2
# This is a nested list where the @{queryResults}[0][0] means the first column from the first row.
@{queryResults} query select a_${i} from test_table where a_1 = 'filename1'
run keyword and continue on failure
... should be equal as Strings
... @{ListA}[${i}]
... @{ListB}[${i}]
... values=False
... msg=In Row "${1}" Column " ${ColumnName}" value ${Excelvalue} != @{queryResults}[0][0].
END