我不知道這是否適合您的需要,但爲什麼不測試這兩個操作,我的意思是測試如果:object1 == object2
和object2 == object1
,通常你應該最終得到相同的值,除非其中一個對象被覆蓋該__eq__
方法,因此將執行這一新__eq__
方法,並返回一個是真的,一個例子是勝於言:
def _assert_just_now(first, second):
"""A Dump function to simulate if two dates are almost equal.
N.B: In this Dump function i will just test if the two datetime object have the
same hour
"""
from datetime import datetime
assert isinstance(first, datetime) and isinstance(second, datetime), \
"This function only accept datetime objects"
return first.hour == second.hour
class Expectation(object):
def __init__(self, assertion, first):
self.assertion = assertion
self.first = first
def __eq__(self, other):
return self.assertion(self.first, other)
def assert_equal(first, second):
"""Usage :
>>> from datetime import datetime
>>> t1 = datetime(year=2007, hour=1, month=3, day=12)
>>> t2 = datetime(year=2011, hour=1, month=5, day=12)
Without using Expectation it's False.
>>> assert_equal(t1, t2)
False
Use the Expectation object.
>>> assert_equal(t1, Expectation(_assert_just_now, t2))
True
Can use Expectation in the first argument too.
>>> assert_equal(Expectation(_assert_just_now, t2), t1)
True
Work also in Container object.
>>> assert_equal({'a': 1, 'b': Expectation(_assert_just_now, t2)},
... {'a': 1, 'b': t1})
True
We change a little bit the values to make the assert equal fail.
>>> t3 = datetime(year=2011, hour=2, month=5, day=12)
>>> assert_equal(t1, t3)
False
This just to make sure that the _assert_just_now doesn't accept object
other than datetime:
>>> assert_equal(t1, Expectation(_assert_just_now, "str"))
Traceback (most recent call last):
...
AssertionError: This function only accept datetime objects
"""
return first == second or second == first
if __name__ == '__main__':
import doctest
doctest.testmod()
希望這可以幫助。
感謝您的回答。那麼,始終保持期望值是一個解決方案,但它打破了習慣:許多人習慣於寫'some_assert(what_i_have,what_i_want_to_have)'。改變這只是爲了期望逆轉不是好主意imho。 – nkrkv 2011-03-13 21:45:33
@nailxx:也許我在答案中不夠清楚:)你可以在doctest中看到,你可以在函數assert_equal的第一個或第二個參數中傳遞期望對象,它會起作用,因爲我'先執行==第二或第二個第一個',那麼你將首先執行'first .__ eq __(second)',如果它不是True,你將執行'second .__ eq __(first)',如果對象的第一個或第二個是'__eq__'記者將運行的期望:) – mouad 2011-03-13 22:34:22
嗯,好吧,你要重新實現'assert_equal'本身。 Uff,替換標準的'TestCase.assertEqual'有點奇怪。但也許這是正確的方向。 – nkrkv 2011-03-14 09:29:24