I'm writing some unit tests, one of which checks that the data provided in a dataframe is of the correct type (float).
When I run the test assertIsInstance(type(foo), np.float64)
the test fails with the following error message: AssertionError <class numpy.float64> is not an instance of <class numpy.float64>
I would have expected this to pass.
test_dataframe_functions.py
import numpy as np
from django.test import TestCase
import pandas as pd
from myapp import dataframe_functions
class DataframeFunctionsTest(TestCase):
dataframe_to_test = dataframe_functions.create_dataframe
#passes
def test_dataframe_is_definitely_a_dataframe(self):
self.assertIsInstance(self.dataframe_to_test, pd.DataFrame)
#passes
def test_dataframe_has_the_right_columns(self):
column_headers = list(self.dataframe_to_test.columns)
self.assertEquals(column_headers, ['header1', 'header2', 'header3'])
#fails with AssertionError <class numpy.float64> is not an instance of <class numpy.float64>
def test_dataframe_header1_is_correct_format(self):
data_instance = self.dataframe_to_test['header1'].iloc[1]
self.assertIsInstance(type(data_instance), np.float64)
I've checked that type(data_instance)
does equal "class numpy.float64" with the following line of code:
print(type(dataframe_to_test['header1'].iloc[1]))
Because a type
object is indeed not an instance of an np.float64
. The assertIsInstance
method should be called with assertIsInstance(object, type)
, and it thus checks if object
is an instance of a (subtype of) type
. A type
object is not an instance of np.float64
.
You thus should call this with:
assertIsInstance(foo, np.float64)
not with:
assertIsInstance(type(foo), np.float64)