如何使用Robot Framework比较两个csv文件

时间:2017-06-07 05:58:08

标签: python python-2.7 csv robotframework

我想比较使用机器人框架的两个csv文件之间的区别。如果存在差异,测试用例应该失败并且如果没有区别则传递。我在Robot框架中尝试过DiffLibrary但是当记录没有被排序时返回传递状态。有人可以指导我如何实现这一目标,或者是否有其他方法可以做到这一点。

我的first.csv文件包含如下数据

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

我的second.csv文件包含如下数据

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

机器人代码在

之下
*** 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

3 个答案:

答案 0 :(得分:0)

如何使用BuiltIn应该与字符串相等?

# 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}

我使用该模型仅用于比较行,如果文件编码存在差异(BOM在文件开头留下特殊的BOM字符等),则可能需要编辑文件字符串。

答案 1 :(得分:0)

这个答案对我有用,即使数据已经排序。如果没有不匹配,它会将结果作为通过,如果数据不匹配,则会失败。

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'

答案 2 :(得分:0)

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