I would like to compare difference between two csv files using robot framework.Test case should fail if there is difference and pass if there is no difference.I have tried DiffLibrary in Robot framework but is returning pass status when records are not sorted. Can someone guide me how this can be achieved or if there is any alternative way to do this.
My first.csv file is having data like below
Benchmark Name,Policy Name,No of Rules,Policy Measurement SLO,Policy Remediation SLO,No of Rules Compliant in MSLO,No of Rules Non-Compliant in RSLO,No of Rules Non-Compliant OUT RSLO
CIS Red Hat Enterprise Linux 6 Benchmark v1.0.0,PCI,281,1 MONTHS,1 MONTHS,150,130
CIS Red Hat Enterprise Linux 7 Benchmark v1.2.0,PCI,281,5 MONTHS,1 MONTHS,150,137
CIS Red Hat Enterprise Linux 7 Benchmark v1.5.0,PCI,281,1 MONTHS,1 MONTHS,150,135
And my second.csv file is having data like below
Benchmark Name,Policy Name,No of Rules,Policy Measurement SLO,Policy Remediation SLO,No of Rules Compliant in MSLO,No of Rules Non-Compliant in RSLO,No of Rules Non-Compliant OUT RSLO
CIS Red Hat Enterprise Linux 7 Benchmark v1.2.0,PCI,281,5 MONTHS,1 MONTHS,150,137
CIS Red Hat Enterprise Linux 6 Benchmark v1.0.0,PCI,281,1 MONTHS,1 MONTHS,150,130
CIS Red Hat Enterprise Linux 7 Benchmark v1.5.0,PCI,281,1 MONTHS,1 MONTHS,150,135
Robot code is below
*** Settings ***
Library DiffLibrary
Library OperatingSystem
*** Test Cases ***
Diffing two files one being different
Run Keyword And Expect Error differences* Diff Files first.csv second.csv
How about just using BuiltIn's Should Be Equal As Strings?
# Using Get File you easily get a file's content into a string variable
${csvA} = Get File ${filePathA}
${csvB} = Get File ${filePathB}
Should Be Equal As Strings ${csvA} ${csvB}
I use that model for comparing lines only, you might need to edit the file string in case there's difference in file encoding (BOM leaves a special BOM character in the beginning of the file, etc.)
This answer worked for me ,even if data is sorted .It will give the result as pass if there is no mismatch and fail if data mismatches.
csv_difference.py
import sys
def csv_diff(file_f,file_g):
#file_f = sys.argv[1]
#file_g = sys.argv[2]
set_f = set()
set_g = set()
with open(file_f) as f:
line = f.readline().strip()
while line:
set_f.add(line)
line = f.readline().strip()
with open(file_g) as g:
line = g.readline().strip()
while line:
set_g.add(line)
line = g.readline().strip()
diff = set_f - set_g
# print set_f
# print set_g
# print diff
if diff:
#print "Data mismatch between the files"
return False
else:
#print " Data Matches "
return True
csv-difference.robot
*** Settings ***
Library OperatingSystem
Library csv_difference.py
*** Test Cases ***
CSV file comparison
${output}= Run keyword csv diff first.csv second.csv
Should Be True '${output}' == 'True'
def csv_diff(file_f,file_g):
with open(file_f) as f:
textf = f.readlines()
textf.pop(1)
set_f = set(textf)
with open(file_g) as g:
textg = g.readlines()
textg.pop(1)
set_g = set(textg)
if set_f == set_g:
return True
return False
basicly, I transfer csv to dict and use jsondeepcompare library to compare csv. thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.