unittest for class Date

Test the class Date we wrote in Class Date and packaged as a separate module in the file named dateunitest.py in Module.

date.py
dateunittest.py

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

Things to try

  1. Make sure nextDay returns None. Change line 41 of dateunittest.py from
                d.nextDay()
    
    to
                self.assertIsNone(d.nextDay())
    
    Ditto for line 55.

  2. If class 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.

    1. Use self.badArgs instead of badArgs.
    2. Use self.now instead of now.
    3. Use self.jan1 instead of jan1.
    4. Use self.daysInYear instead of daysInYear.
    5. Use self.maxMonth instead of datetime.date.max.month.

  3. Have class TestDateMethods test the methods prevDay and prevDays you wrote for class Date in Class Date.

  4. Create a class named 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.