Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read the csv file in robot framework for data verification

I want to read a CSV file for data verification. Any library or Keywords for reading CSV file would do. I am using Robot Framework with Ride.

like image 319
Adnan Ghaffar Avatar asked Mar 21 '23 13:03

Adnan Ghaffar


1 Answers

You can easily create your own library in python for reading and writing csv files. Doing so lets you create any keywords you want. You could simply read and return all the data, or have a keyword that returns the number of rows, or the number of columns, or anything else.

Example keyword to read a csv file:

Save the following definition in a file named csvLibrary.py. It creates a keyword library with a single keyword named "read csv file". Pass is the path to a csv file and it will return the data as a list of lists.

import csv
class csvLibrary(object):

    def read_csv_file(self, filename):
        '''This creates a keyword named "Read CSV File"

        This keyword takes one argument, which is a path to a .csv file. It
        returns a list of rows, with each row being a list of the data in 
        each column.
        '''
        data = []
        with open(filename, 'rb') as csvfile:
            reader = csv.reader(csvfile)
            for row in reader:
                data.append(row)
        return data

Example test:

This test will use the csvLibrary to open up a .csv file, read it, and return the result as a list of lists:

*** Settings ***
| Library | csvLibrary.py

*** Test cases *** 
| Reading a csv file
| | ${data}= | read csv file | test.csv
| | log | ${data}
like image 78
Bryan Oakley Avatar answered Apr 13 '23 20:04

Bryan Oakley