Dive Into Python: call for comments (long)

Steve Purcell stephen_purcell at yahoo.com
Tue Apr 24 02:58:30 EDT 2001


Mark Pilgrim wrote:
> Chapter 4 of "Dive Into Python" ( http://diveintopython.org/ ) is almost
> done, and I would like to devote Chapter 5 to PyUnit (
> http://pyunit.sourceforge.net/ ), the new unit testing framework included in
> Python 2.1.  This is a general call for help to anyone who has had
> experience writing unit tests in other platforms (or worked with PyUnit in
> particular), and anyone who has opinions about my Python coding style and
> The Pythonic Way.


Mark: I'd be happy to help out in any way I can. Just mail me privately.

I've included at the end of this mail an example of how *I* would write
romantest.py.

Some quick comments:
- You should find an example that has a clearer need for 'setUp' -- usually
  setUp methods arise from the refactoring of several long test* methods
  that do a lot of complicated initialisation before they start testing.
- main() is all you need to call in order to get all your tests to run
  from the command-line, provided you name your test methods 'test*'
- assertEqual() already prints out a message "a != b" if it fails. I find
  that writing long assertion messages is not a worthwhile effort because
  the message should hopefully never appear. (More valuable are the names
  of the test cases that fail.)

Best wishes,

-Steve

-- 
Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Any opinions expressed herein are my own and not necessarily those of Yahoo


---------romantest.py-------------------------
#!/usr/bin/python
"""Unit test for roman.py"""

__author__ = "Mark Pilgrim (f8dy at diveintopython.org)"
__version__ = "1.0"
__date__ = "21 April 2001"

import roman
import unittest

#-----------------
#Define test cases
#-----------------

class SanityCheck(unittest.TestCase):
    """test that fromRoman(toRoman(n))==n for all n"""

    def testSanity(self):
        """test that fromRoman(toRoman(n))==n for all n"""
        for n in range(1, 4000):
            s = roman.toRoman(n)
            fullcircle = roman.fromRoman(s)
            self.assertEqual(n, fullcircle, \
                "%(n)s --> %(s)s --> %(fullcircle)s" % locals())

class RomanTestCase(unittest.TestCase):
    """base class to set up tests that require a set of known values"""

    knownValues = ((31, 'XXXI'),
                   (148, 'CXLVIII'),
                   (294, 'CCXCIV'),
                   (312, 'CCCXII'),
                   (421, 'CDXXI'),
                   (528, 'DXXVIII'),
                   (621, 'DCXXI'),
                   (782, 'DCCLXXXII'),
                   (870, 'DCCCLXX'),
                   (941, 'CMXLI'),
                   (1043, 'MXLIII'),
                   (1110, 'MCX'),
                   (1226, 'MCCXXVI'),
                   (1301, 'MCCCI'),
                   (1485, 'MCDLXXXV'),
                   (1509, 'MDIX'),
                   (1607, 'MDCVII'),
                   (1754, 'MDCCLIV'),
                   (1832, 'MDCCCXXXII'),
                   (1993, 'MCMXCIII'),
                   (2074, 'MMLXXIV'),
                   (2152, 'MMCLII'),
                   (2212, 'MMCCXII'),
                   (2343, 'MMCCCXLIII'),
                   (2499, 'MMCDXCIX'),
                   (2574, 'MMDLXXIV'),
                   (2646, 'MMDCXLVI'),
                   (2723, 'MMDCCXXIII'),
                   (2892, 'MMDCCCXCII'),
                   (2975, 'MMCMLXXV'),
                   (3051, 'MMMLI'),
                   (3185, 'MMMCLXXXV'),
                   (3250, 'MMMCCL'),
                   (3313, 'MMMCCCXIII'),
                   (3408, 'MMMCDVIII'),
                   (3501, 'MMMDI'),
                   (3610, 'MMMDCX'),
                   (3743, 'MMMDCCXLIII'),
                   (3844, 'MMMDCCCXLIV'),
                   (3940, 'MMMCMXL'))

    def testConversionToKnownValues(self):
        """test toRoman with known values"""
        for n, s in self.knownValues:
            result = roman.toRoman(n)
            self.assertEqual(s, result)

    def testConversionFromKnownValues(self):
        """test fromRoman with known values"""
        for n, s in self.knownValues:
            result = roman.fromRoman(s)
            self.assertEqual(n, result)


class ToRomanBadInput(unittest.TestCase):
    """test toRoman with a variety of bad inputs"""

    def testTooLarge(self):
        """test toRoman with large input"""
        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 4000)
    def testZero(self):
        """test toRoman with 0 input"""
        self.assertRaises(roman.OutOfRangeError, roman.toRoman, 0)

    def testNegative(self):
        """test toRoman with negative input"""
        self.assertRaises(roman.OutOfRangeError, roman.toRoman, -1)

    def testDecimal(self):
        """test toRoman with non-integer input"""
        self.assertRaises(roman.NotIntegerError, roman.toRoman, 0.5)

class FromRomanBadInput(unittest.TestCase):
    """test fromRoman with a variety of bad inputs"""

    def testTooManyRepeatedNumerals(self):
        """test fromRoman with too many repeated numerals"""
        for s in ('MMMM', 'DD', 'CCCC', 'LL', 'XXXX', 'VV', 'IIII'):
            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)

    def testRepeatedPairs(self):
        """test fromRoman with repeated pairs of numerals"""
        for s in ('CMCM', 'CDCD', 'XCXC', 'XLXL', 'IXIX', 'IVIV'):
            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)

    def testMalformedAntecedent(self):
        """test fromRoman with malformed antecedents"""
        for s in ('IIMXCC', 'VX', 'DCM', 'CMM', 'IXIV',
                  'MCMC', 'XCX', 'IVI', 'LM', 'LD', 'LC'):
            self.assertRaises(roman.InvalidRomanNumeralError, roman.fromRoman, s)


if __name__ == '__main__':
    unittest.main()




More information about the Python-list mailing list