本文介绍了Python 3.3:使用nase.tools.assert_equals时的DeprecationWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用鼻子测试工具来声明python unittest :

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)

    ...

当我运行 python setup.py test 时,我会收到弃用警告:

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
...

我在鼻子工具中找不到任何 assertEqual .该警告来自何处,我该如何解决?

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

推荐答案

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

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

但是,后者只是 TestCase.assertEqual()别名(请注意:没有结尾的 s ).该警告旨在告诉您,您需要使用 TestCase.assertEquals()代替 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.

对于使用 assert_equal 转换为(没有尾随 s )的 nose.tools :

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)

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

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

这篇关于Python 3.3:使用nase.tools.assert_equals时的DeprecationWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 11:31