I have done a search but could not find what I was after.
My code is as followed:
import datetime
import doctest
import os
def parseOptions():
import optparse
parser = optparse.OptionParser(usage= '-h')
parser.add_option('-d', '--difference', \
type= 'int')
(options, args) = parser.parse_args()
return options
now = datetime.datetime.now()
subtract = datetime.timedelta(days=10, hours=11)
difference = now - subtract
def checktime(now - b):
"""Returns a - b
>>> checktime(now - 10)
'now - 10'
>>> checktime(now - 20)
'now - 20'
>>> checktime(now - 30)
'now - 30'
"""
return now - b
if__name__== "__main__":
doctest.testmod()
print
print 'The time now is =', now.strftime("%I:%M:%S%p %a, %B %d %Y")
print 'The time minus the difference =', difference.strftime("%I:%M:%S%p %a, %B %d %Y")
print
I want the doctest to test that the time now minus the number (10, 20, 30 in hours) is giving the correct output before proceeding.
When I run the script in command prompt I get the following mesage:
2 items had no tests:
time
time.struct_time
0 tests in 2 items.
0 passed and 0 failed.
Test passed.
I have attempted to changes bits and pieces to find what is being defined with time and time.struct_time and have failed miserably.
Any help would be greatly appreciated!
Doctest is useful if you want to test that the function is doing what was intended. In your case, I presume you want to subtract b
hours from the datetime
argument and return the difference.
Re-writing your function to subtime
def subtime(a,b):
subtract = datetime.timedelta(hours=b)
difference = a - subtract
return difference
To run doctests, you'll need to provide some sample calling values and the expected results. Think about the corner cases for your function (Places where something wierd might happen). Now let us form some test-cases
Now write down how the function will be called in each case. Use the same datetime
as first argument for simplicity. (Do not use script variables such as now
because you cannot predict their values when the script runs)
subtime(datetime.datetime(2013,11,11,11,0),10)
subtime(datetime.datetime(2013,11,11,11,0),24)
subtime(datetime.datetime(2013,11,11,11,0),30)
subtime(datetime.datetime(2013,11,11,11,0),0)
subtime(datetime.datetime(2013,11,11,11,0),-5)
Now calculate (using a pen and paper) what should be the ideal result for each case
datetime.datetime(2013, 11, 11, 1, 0)
datetime.datetime(2013, 11, 10, 11, 0)
datetime.datetime(2013, 11, 10, 5, 0)
datetime.datetime(2013, 11, 11, 11, 0)
datetime.datetime(2013, 11, 11, 16, 0)
Add this to your function in the documentation string """ """
def subtime(a,b):
""" (datetime,int) -> datetime
Subtract b hours from a datetime.datetime and return the new datetime object
>>> subtime(datetime.datetime(2013,11,11,11,0),10)
datetime.datetime(2013, 11, 11, 1, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),24)
datetime.datetime(2013, 11, 10, 11, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),30)
datetime.datetime(2013, 11, 10, 5, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),0)
datetime.datetime(2013, 11, 11, 11, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),-5)
datetime.datetime(2013, 11, 11, 16, 0)
"""
subtract = datetime.timedelta(hours=b)
difference = now - subtract
return difference
Your script now is
import datetime
import doctest
import os
def parseOptions():
import optparse
parser = optparse.OptionParser(usage= '-h')
parser.add_option('-d', '--difference', \
type= 'int')
(options, args) = parser.parse_args()
return options
now = datetime.datetime.now()
def subtime(a,b):
""" (datetime,int) -> datetime
Subtract b hours from a datetime.datetime and return the new datetime object
>>> subtime(datetime.datetime(2013,11,11,11,0),10)
datetime.datetime(2013, 11, 11, 1, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),24)
datetime.datetime(2013, 11, 10, 11, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),30)
datetime.datetime(2013, 11, 10, 5, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),0)
datetime.datetime(2013, 11, 11, 11, 0)
>>> subtime(datetime.datetime(2013,11,11,11,0),-5)
datetime.datetime(2013, 11, 11, 16, 0)
"""
subtract = datetime.timedelta(hours=b)
difference = a - subtract
return difference
if __name__== "__main__":
doctest.testmod()
print
print 'This is the time now -', now.strftime("%I:%M:%S %p %a, %B %d %Y")
difference=subtime(now,10)
print 'This is the time minus the difference -', difference.strftime("%I:%M:%S %p %a, %B %d %Y")
print
And the output
$ python try.py
This is the time now - 02:40:57 PM Tue, May 28 2013
This is the time minus the difference - 04:40:57 AM Tue, May 28 2013
Note that with doctest you won't get any test related output if all the tests pass. (Everything is OK)