这是我的秒表课
import time
class StopWatch:
def __init__(self, tag="", startTime=time.time(), elapsedTime=-1):
self._tag=tag
self._elapsedTime=elapsedTime
self._startTime=startTime
# other codes here............#
def stop(self, tag=None):
if tag is not None:
self._tag = tag
self._elapsedTime = time.time() - self._startTime
return self
def getStartTime(self):
return self._startTime
秒表测试文件是:
import unittest
import time
from metric.StopWatch import StopWatch
class StopWatchTest(unittest.TestCase):
def test_stopwatch(self):
now=time.time()
print "now:%f" % now
stopwatch=StopWatch("firstHook")
print "start time:%r" % stopwatch.getStartTime()
self.assertTrue(stopwatch.getStartTime()>now, "start time is wrong")
我得到的是错误:
now:1364791630.047630
start time:1364791629.158797
..
AssertionError: start time is wrong
稍后执行的代码显示更早的时间。这背后的原因是什么?