我正在使用 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。这个警告来自哪里,我该如何解决?

最佳答案

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

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

对于 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)

如果您使用 assert_almost_equals (带有尾随 s ),您也会看到类似的警告以使用 assertAlmostEqual

关于Python 3.3 : DeprecationWarning when using nose. tools.assert_equals,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23040166/

10-16 00:31