Reputation: 49
I have two psv (pipe-separated) files, I need to compare the data columnwise.
How can I compare those two files, is it possible to compare two psv files through Robot??
Upvotes: 1
Views: 2832
Reputation: 386295
There is nothing built-in to the robot framework specifically for psv files. However, assuming they are text files, you can write a keyword in python that compares the two files. Python has a csv module which makes it easy to parse such files, assuming your data is well-formed.
For example, create a file named PsvLibrary.py
with the following contents. Be aware: this isn't production-quality code. It assumes the data is well formed and that each file has exactly the same number of rows, and the number of rows isn't gargantuan. The point is to show the general idea, not to give a fully functioning library:
import csv
class PsvLibrary(object):
def compare_columns(self, column_number, filea, fileb):
with open(filea, "rb") as f:
reader = csv.reader(f, delimiter="|")
filea_data = [line for line in reader]
with open(fileb, "rb") as f:
reader = csv.reader(f, delimiter="|")
fileb_data = [line for line in reader]
# for the given column, make sure the data in file a
# matches the data in file b
c = int(column_number)
for n, (row_a, row_b) in enumerate(zip(filea_data, fileb_data)):
if row_a[c] != row_b[c]:
raise Exception("line %d: '%s' != '%s'" % (n+1, row_a[c], row_b[c]))
You can then use this library in a test case like this:
*** Settings ***
| Library | PsvLibrary
*** Test Cases ***
| Example of comparing columns in pipe-separated files
| | compare columns | 1 | file1.psv | file2.psv
For testing, create a file named file1.psv
with the following data:
apple|red
orange|orange
grape|green
Create a second file named file2.psv
with the following data. Note that the last line is different in the second column:
apple|red
orange|orange
grape|purple
If you run the above test you should see this as the output:
==============================================================================
Example
==============================================================================
Example of reading a pipe-separated file | FAIL |
line 3: 'green' != 'purple'
Upvotes: 3