我正在使用 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_equals
与 TestCase.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/