Python 3.3: 使用nose.tools.assert_equals时出现DeprecationWarning

6

我正在使用nose测试工具来断言一个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)

    ...

当我运行python setup.py test时,我收到了一个弃用警告:
...
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
...

我在nose工具中找不到任何assertEqual。该警告是从哪里来的?我该如何解决这个问题?
1个回答

11

nose.tools中的assert_*函数只是自动创建的PEP8别名,用于TestCase方法,因此assert_equalsTestCase.assertEquals()相同。

然而,后者只是TestCase.assertEqual()的一个别名(注意:没有尾随的s)。该警告旨在告诉您,您需要使用TestCase.assertEqual()而不是TestCase.assertEquals(),因为该alias has been deprecated已被弃用。

对于nose.tools,这意味着使用assert_equal(没有尾随的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)

如果您使用了带有结尾的sassert_almost_equals,您将会看到类似的警告,提示您使用 assertAlmostEqual


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接