问题:
我们使用nose测试运行程序已经有一段时间了。
有时,我看到我们的测试具有eq_()调用:
eq_(actual, expected)而不是普通的:
self.assertEqual(actual, expected)问题:
与标准的统一框架的nose.tools.eq_相比,使用assertEqual()有什么好处吗?它们实际上是等同的吗?
Thoughts:
首先,eq_更短,但是它必须从nose.tools导入,这使得测试依赖于测试运行库,这会使切换到不同的测试运行程序变得更加困难,py.test说。另一方面,我们也经常使用@istest、@nottest和@attr鼻子装饰器。
发布于 2015-07-31 22:48:48
它们不等同于unittest.TestCase.assertEqual。
nose.tools.ok_(expr, msg=None)assert的简称。保存3个完整字符!nose.tools.eq_(a, b, msg=None)assert a == b, "%r != %r" % (a, b)的简称
然而,这些文档有点误导人。如果您检查源代码,您将看到eq_实际上是:
def eq_(a, b, msg=None):
if not a == b:
raise AssertionError(msg or "%r != %r" % (a, b))https://github.com/nose-devs/nose/blob/master/nose/tools/trivial.py#L25
这与assertEqual的基本情况非常接近。
def _baseAssertEqual(self, first, second, msg=None):
"""The default assertEqual implementation, not type specific."""
if not first == second:
standardMsg = '%s != %s' % _common_shorten_repr(first, second)
msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg) # default: AssertionError然而,正如docstring和函数名所暗示的那样,assertEqual具有类型特定的潜力。这是您在eq_ (或assert a == b )中丢失的东西。unittest.TestCase为dicts、lists、tuples、sets、frozensets和strs提供了特例,它们似乎更有利于更漂亮地打印错误消息。
但是assertEqual是TestCase的类成员,所以它只能在TestCases中使用,nose.tools.eq_可以在任何地方使用,就像一个简单的assert。
https://stackoverflow.com/questions/31730480
复制相似问题