Reputation: 905
I am trying to compare results of two lists using assert statement to test the function using pytest. Whenever I try to compare using assert statement, my expected result is automatically converted to a string with double quotes. This is so far I have done:
# read data from xlsx file
book = xlrd.open_workbook("nameEntityDataSet.xlsx")
first_sheet = book.sheet_by_index(0) # get first worksheet
# take second element(index starts from 0) from first row
se1 = first_sheet.row_values(1)[1]
print "se1 ", se1 #returns me list:
# take third element(index 2) from first row
se2 = first_sheet.row_values(1)[2]
expected_output = first_sheet.row_values(1)[3]
#expected output is printed as: [['UAE', 'United'], ['UAE', 'Arab'], ['UAE', 'Emirates']]
current_output = [['UAE', 'United'], ['UAE', 'Arab'], ['UAE', 'Emirates']]
#When I do assert to compare both outputs
assert current_output == expected_output
Returns this error:
assert [['UAE', 'United'], ['UAE', 'Arab'], ['UAE', 'Emirates']] == "[['UAE', 'United'], ['UAE', 'Arab'], ['UAE', 'Emirates']]" test_wordAligner.py:15: AssertionError
I do not know why it adds double quotation marks on expected output. Due to this reason I am getting AssertionError. I have searched a lot but did not find any helpful answer in my case. Please note that I want solution in pytest not unittest
Thanks in advance
Upvotes: 0
Views: 2415
Reputation: 9997
Simply because expected_output
is a string and your asserting against a list.
expected_output = first_sheet.row_values(1)[3]
That line returns a string. Since you accessing one particular item of the list returned by row_values(1)
. In order, to help you debug, try to:
print(type(expected_output))
This will show you that expected_output
is a string at the moment.
Now, in order to trasnform it to list, here is something you could try:
import ast #put this at the top of your file
expected_output = first_sheet.row_values(1)[3]
expected_output = ast.literal_eval(expected_output)
Then you can compare both values.
Upvotes: 2