Python 3.3 : DeprecationWarning when using nose. tools.assert_equals

标签 python deprecated python-3.3 nose python-unittest

我正在使用 nosetest 工具断言 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()别名(注意:没有尾随 ).该警告旨在告诉您,您需要使用 TestCase.assertEqual() 作为 alias has been deprecated 而不是 TestCase.assertEquals() .

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

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(带有尾随 ),您也会看到类似的警告以使用 assertAlmostEqual

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

相关文章:

node.js - Youtube API v3,topicId 恢复

python - 如何在 Windows 7 上安装适用于 Python 3.5 或 3.3 的 pyodbc

python os.listdir 不显示所有文件

python - 使用正则表达式删除 pandas 列中的特殊字符

ios - 使用新的 swift os_log api 读取日志

python 连接到 PostgreSQL 时无法使用 .pgpass

python - python标准库中的装饰器(特别是@deprecated)

python - 为什么终止此子进程会引发 ProcessLookupError?

python - 如何在 python nmap 中提供 inputfilefile(-iL) 选项?

python - 删除不用作外键的记录的通用解决方案