如何在没有太多负担的情况下使用 `assertEqual()` [或等价物]?
How to use `assertEqual()` [or equivalent] without much baggage?
我正在寻找一种可以比较两个值并在比较失败时通过有意义的消息引发断言错误的方法(如果可用)。
如果我使用 assert
,失败消息不包含断言失败时比较的值。
>>> a = 3
>>> b = 4
>>> assert a == b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>
如果我使用 unittest.TestCase
包中的 assertEqual()
方法,断言消息包含比较的值。
a = 3
b = 4
> self.assertEqual(a, b)
E AssertionError: 3 != 4
请注意,此处断言错误消息包含比较的值。这在现实生活场景中非常有用,因此对我来说是必要的。普通 assert
(见上文)不会那样做。
但是,到目前为止,我只能在继承自 unittest.TestCase
的 class 中使用 assertEqual()
,并提供了很少的其他所需方法,如 runTest()
。我想在任何地方使用 assertEqual()
,而不仅仅是在继承的 class 中。那可能吗?
我尝试了以下但它们没有用。
>>> import unittest
>>> unittest.TestCase.assertEqual(a, b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method failUnlessEqual() must be called with TestCase instance as first argument (got int instance instead)
>>>
>>>
>>> tc = unittest.TestCase()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/unittest.py", line 215, in __init__
(self.__class__, methodName)
ValueError: no such test method in <class 'unittest.TestCase'>: runTest
>>>
是否有任何其他包或库提供类似 assertEqual()
的方法,可以在没有额外限制的情况下轻松使用?
您必须手动给出断言信息:
assert a == b, '%s != %s' % (a, b)
# AssertionError: 3 != 4
你看过numpy.testing了吗?
https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.testing.html
其中包括:
assert_almost_equal(actual, desired[ ...]) 如果两个项目不等于所需精度,则引发 AssertionError。
这个断言打印出实际的和期望的。如果你提高精度,比较任意接近 == (对于浮点数)
可以创建一个 "helper" 新模块来提供对断言函数的访问。 AssertsAccessor
在这种情况下:
from unittest import TestCase
# Dummy TestCase instance, so we can initialize an instance
# and access the assert instance methods
class DummyTestCase(TestCase):
def __init__(self):
super(DummyTestCase, self).__init__('_dummy')
def _dummy(self):
pass
# A metaclass that makes __getattr__ static
class AssertsAccessorType(type):
dummy = DummyTestCase()
def __getattr__(cls, key):
return getattr(AssertsAccessor.dummy, key)
# The actual accessor, a static class, that redirect the asserts
class AssertsAccessor(object):
__metaclass__ = AssertsAccessorType
模块只需要创建一次,然后 unittest
包中的所有 asserts 都可以访问,例如:
AssertsAccessor.assertEquals(1, 2)
AssertionError: 1 != 2
或者另一个例子:
AssertsAccessor.assertGreater(1, 2)
结果:
AssertionError: 1 not greater than 2
假设为访问器创建的模块名为 assertions
,代码中的常见用法如下:
from assertions import AssertsAccessor
def foo(small_arg, big_arg):
AssertsAccessor.assertGreater(big_arg, small_arg)
# some logic here
我过去也有过类似的经历,最后写了一个简短的自定义断言,它接受任何条件作为输入。
import inspect
def custom_assert(condition):
if not condition:
frame = inspect.currentframe()
frame = inspect.getouterframes(frame)[1]
call_signature = inspect.getframeinfo(frame[0]).code_context[0].strip()
import re
argument = re.search('\((.+)\)', call_signature).group(1)
if '!=' in argument:
argument = argument.replace('!=','==')
elif '==' in argument:
argument = argument.replace('==','!=')
elif '<' in argument:
argument = argument.replace('<','>=')
elif '>' in argument:
argument = argument.replace('>','<=')
elif '>=' in argument:
argument = argument.replace('>=','<')
elif '<=' in argument:
argument = argument.replace('<=','>')
raise AssertionError(argument)
if __name__ == '__main__':
custom_assert(2 == 1)
输出:
Traceback (most recent call last):
File "custom_assert.py", line 27, in <module>
custom_assert(2 == 1)
File "custom_assert.py", line 24, in custom_assert
raise AssertionError(argument)
AssertionError: 2 != 1
assertEqual 或任何其他 assertXxx() 方法要求第一个参数是对象引用。一般我们称该方法为self.assertEqual(first, second, msg=None)
。
这里 self
满足第一个预期参数。为了避免这种情况,我们可以这样做:
from unittest import TestCase as tc
def some_func():
dummy_obj = tc()
tc.assertEqual(dummy_obj, 123, 123, msg='Not Equal')
此行为的原因是 XUnit 框架的宿醉。
我正在寻找一种可以比较两个值并在比较失败时通过有意义的消息引发断言错误的方法(如果可用)。
如果我使用 assert
,失败消息不包含断言失败时比较的值。
>>> a = 3
>>> b = 4
>>> assert a == b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>
如果我使用 unittest.TestCase
包中的 assertEqual()
方法,断言消息包含比较的值。
a = 3
b = 4
> self.assertEqual(a, b)
E AssertionError: 3 != 4
请注意,此处断言错误消息包含比较的值。这在现实生活场景中非常有用,因此对我来说是必要的。普通 assert
(见上文)不会那样做。
但是,到目前为止,我只能在继承自 unittest.TestCase
的 class 中使用 assertEqual()
,并提供了很少的其他所需方法,如 runTest()
。我想在任何地方使用 assertEqual()
,而不仅仅是在继承的 class 中。那可能吗?
我尝试了以下但它们没有用。
>>> import unittest
>>> unittest.TestCase.assertEqual(a, b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method failUnlessEqual() must be called with TestCase instance as first argument (got int instance instead)
>>>
>>>
>>> tc = unittest.TestCase()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/unittest.py", line 215, in __init__
(self.__class__, methodName)
ValueError: no such test method in <class 'unittest.TestCase'>: runTest
>>>
是否有任何其他包或库提供类似 assertEqual()
的方法,可以在没有额外限制的情况下轻松使用?
您必须手动给出断言信息:
assert a == b, '%s != %s' % (a, b)
# AssertionError: 3 != 4
你看过numpy.testing了吗?
https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.testing.html
其中包括: assert_almost_equal(actual, desired[ ...]) 如果两个项目不等于所需精度,则引发 AssertionError。
这个断言打印出实际的和期望的。如果你提高精度,比较任意接近 == (对于浮点数)
可以创建一个 "helper" 新模块来提供对断言函数的访问。 AssertsAccessor
在这种情况下:
from unittest import TestCase
# Dummy TestCase instance, so we can initialize an instance
# and access the assert instance methods
class DummyTestCase(TestCase):
def __init__(self):
super(DummyTestCase, self).__init__('_dummy')
def _dummy(self):
pass
# A metaclass that makes __getattr__ static
class AssertsAccessorType(type):
dummy = DummyTestCase()
def __getattr__(cls, key):
return getattr(AssertsAccessor.dummy, key)
# The actual accessor, a static class, that redirect the asserts
class AssertsAccessor(object):
__metaclass__ = AssertsAccessorType
模块只需要创建一次,然后 unittest
包中的所有 asserts 都可以访问,例如:
AssertsAccessor.assertEquals(1, 2)
AssertionError: 1 != 2
或者另一个例子:
AssertsAccessor.assertGreater(1, 2)
结果:
AssertionError: 1 not greater than 2
假设为访问器创建的模块名为 assertions
,代码中的常见用法如下:
from assertions import AssertsAccessor
def foo(small_arg, big_arg):
AssertsAccessor.assertGreater(big_arg, small_arg)
# some logic here
我过去也有过类似的经历,最后写了一个简短的自定义断言,它接受任何条件作为输入。
import inspect
def custom_assert(condition):
if not condition:
frame = inspect.currentframe()
frame = inspect.getouterframes(frame)[1]
call_signature = inspect.getframeinfo(frame[0]).code_context[0].strip()
import re
argument = re.search('\((.+)\)', call_signature).group(1)
if '!=' in argument:
argument = argument.replace('!=','==')
elif '==' in argument:
argument = argument.replace('==','!=')
elif '<' in argument:
argument = argument.replace('<','>=')
elif '>' in argument:
argument = argument.replace('>','<=')
elif '>=' in argument:
argument = argument.replace('>=','<')
elif '<=' in argument:
argument = argument.replace('<=','>')
raise AssertionError(argument)
if __name__ == '__main__':
custom_assert(2 == 1)
输出:
Traceback (most recent call last):
File "custom_assert.py", line 27, in <module>
custom_assert(2 == 1)
File "custom_assert.py", line 24, in custom_assert
raise AssertionError(argument)
AssertionError: 2 != 1
assertEqual 或任何其他 assertXxx() 方法要求第一个参数是对象引用。一般我们称该方法为self.assertEqual(first, second, msg=None)
。
这里 self
满足第一个预期参数。为了避免这种情况,我们可以这样做:
from unittest import TestCase as tc
def some_func():
dummy_obj = tc()
tc.assertEqual(dummy_obj, 123, 123, msg='Not Equal')
此行为的原因是 XUnit 框架的宿醉。