how to use assert_frame_equal in unittest
alecxe answer is incomplete, you can indeed use pandas' assert_frame_equal()
with unittest.TestCase
, using unittest.TestCase.addTypeEqualityFunc
import unittest
import pandas as pd
import pandas.testing as pd_testing
class TestSplitWeight(unittest.TestCase):
def assertDataframeEqual(self, a, b, msg):
try:
pd_testing.assert_frame_equal(a, b)
except AssertionError as e:
raise self.failureException(msg) from e
def setUp(self):
self.addTypeEqualityFunc(pd.DataFrame, self.assertDataframeEqual)
def test_allZero(self):
self.assertEqual(pd.DataFrame([0,0,0,0]), pd.DataFrame([0,0,0,0]))
assert_frame_equal()
is coming from the pandas.util.testing
package, not from the unittest.TestCase
class. Replace:
self.assert_frame_equal(pd.DataFrame([0,0,0,0]),pd.DataFrame([0,0,0,0]))
with:
assert_frame_equal(pd.DataFrame([0,0,0,0]), pd.DataFrame([0,0,0,0]))
When you had self.assert_frame_equal
, it tried to find assert_frame_equal
attribute on the unittest.TestCase
instance, and, since there is not assert_frame_equal
attribute or method exposed on an unittest.TestCase
class, it raised an AttributeError
.