Home » excel » python 2.7 – Unit test for reading an excel file witth pandas

python 2.7 – Unit test for reading an excel file witth pandas

Posted by: admin May 14, 2020 Leave a comment


I need to write a unit test case for the below code :

    def read_data(self, data):

    """Read data from excel file.

    :param data:str, data in file
    :return:str, data after reading excel file
        read_data = pd.read_excel(data)
        return read_data
    except Exception as e:
        logger.info("Not able to read data. Error :- {}".format(e))
        raise e

I am reading an excel file in the above code, which gives me data like this:
refer screenshot.

So, How to store the above data after reading from excel sheet as dummy data so that I can assert it to my original data?


How to&Answers:

Necroposting this because I had the same need.

this answer can point you in the right direction:

See also Saving the Dataframe output to a string in the XlsxWriter docs.

From the example you can build something like this:

import pandas as pd
import io

# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

output = io.BytesIO()

# Use the BytesIO object as the filehandle.
writer = pd.ExcelWriter(output, engine='xlsxwriter')

# Write the data frame to the BytesIO object.
df.to_excel(writer, sheet_name='Sheet1', index=False)

# Read the BytesIO object back to a data frame - here you should use your method
xlsx_data = pd.read_excel(output)

# Assert that the data frame is the same as the original
pd.testing.assert_frame_equal(xlsx_data, df)

Basically you flip the problem around: you build a data frame with some data in it, save it in a temporary file-like object, pass that object to your method, and then assert that the data is the same as the one you created.

NOTE: It needs pandas 0.17+