What you do is not a unit test . Unit tests should use a small “block” of your code and only your code. By turning on system time, you are also testing the environment in which you are currently working. This is a system test . Unit tests are very effective tools to make sure your code is written correctly, but it helps many of you write your code in a “verifiable” way.
, , , . , , "" , " stub" " mock objects" .
, , - , . . , , , . , - . ( Google Testing , unit test , .) - , , , , .. , .
. , . , , . 2 3 " " . Python , OO-:
class MyClass(object):
def _get_current_time(self):
'''This is a test seam'''
return datetime.datetime.now()
def age(self):
return self._get_current_time() - self._birthday
:
class FakeMyClass(MyClass):
def __init__(self, test_time, *args, **kwargs):
self._test_time = test_time
MyClass.__init__(self, *args, **kwargs)
def _get_current_time(self)
return self._test_time
, FakeMyClass, , :
myclass = FakeMyClass(t)
self.assertEqual(myclass.age(), expected_age)
, , .