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
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'