Test the class
Date
we wrote in
Class Date
and packaged as a separate module in the file named
dateunitest.py
in
Module.
test_init (__main__.TestDateMethods) Does date.__init__ construct the correct object? ... ok test_init_raise (__main__.TestDateMethods) Does date.Date.__init__ raise exceptions correctly? ... ok test_monthsInYear (__main__.TestDateMethods) Does date.monthsInYear return the correct value? ... ok test_nextDay (__main__.TestDateMethods) Does date.nextDay increment the Date object? ... ok test_nextDays (__main__.TestDateMethods) Does date.nextDays advance the Date object? ... ok test_nextDays_raise (__main__.TestDateMethods) Does date.Date.nextDays raise exceptions correctly? ... ok ---------------------------------------------------------------------- Ran 6 tests in 42.230s
nextDay
returns
None.
Change
line
41
of
dateunittest.py
from
d.nextDay()
to
self.assertIsNone(d.nextDay())
Ditto for
line
55.
TestDateMethods
has a method with the special name
setUp,
the method will be called automatically before the first
test_
method is called.
We can use this to consolidate duplicate code in the
test_
methods.
Insert the following method at
line
12
of
dateunittest.py.
def setUp(self):
"Create instance attributes for test_ methods to use."
self.badArgs = (None, "", 1.0, (), [], {}) #only int is legal
self.now = datetime.datetime.now() #so we can get the current year
self.jan1 = datetime.date(self.now.year, 1, 1)
self.daysInYear = TestDateMethods.daysInYear(self.now.year)
self.maxMonth = datetime.date.max.month
In the
test_
methods of class
TestDateMethods,
remove the statements that create variables named
badArgs,
now,
jan1,
and
daysInYear.
self.badArgs
instead of
badArgs.
self.now
instead of
now.
self.jan1
instead of
jan1.
self.daysInYear
instead of
daysInYear.
self.maxMonth
instead of
datetime.date.max.month.
TestDateMethods
test the methods
prevDay
and
prevDays
you wrote for class
Date
in
Class Date.
Time
in a file named
time.py,
analogous to our class
Date
in the file
date.py.
The instance attributes in a
Time
object should be
hour,
minute,
and
second
instead of
year,
month,
and
day.
Then write a unit test class for class
Time.