Python 3.3: DeprecationWarning when using nose.tools.assert_equals

坚强是说给别人听的谎言 提交于 2021-01-27 13:26:31

问题


I am using nosetest tools for asserting a python unittest:

...
from nose.tools import assert_equals, assert_almost_equal

class TestPolycircles(unittest.TestCase):

    def setUp(self):
        self.latitude = 32.074322
        self.longitude = 34.792081
        self.radius_meters = 100
        self.number_of_vertices = 36
        self.vertices = polycircles.circle(latitude=self.latitude,
                                           longitude=self.longitude,
                                           radius=self.radius_meters,
                                           number_of_vertices=self.number_of_vertices)

    def test_number_of_vertices(self):
        """Asserts that the number of vertices in the approximation polygon
        matches the input."""
        assert_equals(len(self.vertices), self.number_of_vertices)

    ...

When I run python setup.py test, I get a deprecation warning:

...
Asserts that the number of vertices in the approximation polygon ...
/Users/adamatan/personal/polycircles/polycircles/test/test_polycircles.py:22:    
DeprecationWarning: Please use assertEqual instead.
  assert_equals(len(self.vertices), self.number_of_vertices)
ok
...

I could not find any assertEqual in nose tools. Where is this warning coming from, and how can I fix it?


回答1:


The nose.tools assert_* functions are just automatically created PEP8 aliases for the TestCase methods, so assert_equals is the same as TestCase.assertEquals().

However, the latter was only ever an alias for TestCase.assertEqual() (note: no trailing s). The warning is meant to tell you that instead of TestCase.assertEquals() you need to use TestCase.assertEqual() as the alias has been deprecated.

For nose.tools that translates into using assert_equal (no trailing s):

from nose.tools import assert_equal, assert_almost_equal

def test_number_of_vertices(self):
    """Asserts that the number of vertices in the approximation polygon
    matches the input."""
    assert_equal(len(self.vertices), self.number_of_vertices)

Had you used assert_almost_equals (with trailing s), you'd have seen a similar warning to use assertAlmostEqual, as well.



来源:https://stackoverflow.com/questions/23040166/python-3-3-deprecationwarning-when-using-nose-tools-assert-equals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!