Python 3.3:使用nase.tools.assert_equals时的DeprecationWarning

亚当·马坦(Adam Matan)|

我正在使用鼻子测试工具来声明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
...

assertEqual在鼻工具中找不到任何东西该警告从何而来,我该如何解决?

马丁·彼得斯(Martijn Pieters)

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

然而,后者是有史以来唯一一个别名TestCase.assertEqual()(注:没有结尾s)。该警告旨在告诉您,由于别名已被弃用,因此TestCase.assertEquals()您无需使用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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章