How to use assert_frame_equal in unittest

New unittest package. I am trying to check the DataFrame returned by a function through the following code. Despite the fact that I hardcoded the inputs with assert_frame_equalequal ( pd.DataFrame([0,0,0,0])), unittest still does not work. Would anyone like to explain why this is happening?

import unittest
from pandas.util.testing import assert_frame_equal
class TestSplitWeight(unittest.TestCase):
    def test_allZero(self):
        #splitWeight(pd.DataFrame([0,0,0,0]),10)
        self.assert_frame_equal(pd.DataFrame([0,0,0,0]),pd.DataFrame([0,0,0,0]))

suite = unittest.TestLoader().loadTestsFromTestCase(TestSplitWeight)
unittest.TextTestRunner(verbosity=2).run(suite)
Error: AttributeError: 'TestSplitWeight' object has no attribute 'assert_frame_equal'
+10
source share
2 answers

assert_frame_equal()comes from a package pandas.util.testing, not from a class unittest.TestCase. 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]))

self.assert_frame_equal, assert_frame_equal unittest.TestCase , assert_frame_equal, unittest.TestCase, , AttributeError.

+9

alecxe , assert_frame_equal() unittest.TestCase, 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]))
+8

Source: https://habr.com/ru/post/1650600/


All Articles