它们不等同于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)的简写
https://nose.readthedocs.org/en/latest/testing_tools.html#nose.tools.ok_
然而,这些文档有点误导。如果您查看源代码,您会看到 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
https://github.com/python/cpython/blob/9b5ef19c937bf9414e0239f82aceb78a26915215/Lib/unittest/case.py#L805
但是,正如文档字符串和函数名称所暗示的那样,assertEqual 有可能是特定于类型的。这是您使用eq_(或assert a == b,就此而言)失去的东西。 unittest.TestCase 有 dicts、lists、tuples、sets、frozensets 和 strs 的特殊情况。这些似乎有助于更漂亮地打印错误消息。
但是assertEqual是TestCase的类成员,所以只能在TestCases中使用。 nose.tools.eq_ 可以在任何地方使用,例如简单的assert。